[英]Regain Focus on EditText when User Clicks It
I have an activity wiht some EditText, and I want the following behaviour for one of the EditText: on activity starting-up, the EditText is populated with some text. 我有一个带有一些EditText的活动,并且我希望其中一个EditText具有以下行为:在活动启动时,EditText会填充一些文本。 It should not show cursor, has no focus and not show keyboard (so user can just read the text and scroll up and down). 它不应该显示光标,没有焦点并且不显示键盘(因此用户可以阅读文本并向上和向下滚动)。 Then when the user clicks/touches the EditText, it will gain focus, show cursor and show keyboard (so user can start editing). 然后,当用户单击/触摸EditText时,它将获得焦点,显示光标和显示键盘(以便用户可以开始编辑)。
The behaviour at starting up is working OK, but I cannot get the "start editing" behaviour to work when user clicks on the EditText. 启动时的行为正常,但是当用户单击EditText时,我无法使“开始编辑”行为正常。
Here is my code: 这是我的代码:
activity_main.xml: ... activity_main.xml: ...
...
<EditText
android:id="@+id/notepad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="@android:color/transparent"
android:textSize="@dimen/font_large"
android:textStyle="normal"
android:textColor="@color/appPrimaryText"
android:textColorHint="@color/appAccentColour"
android:gravity="top"
android:scrollbars="vertical" />
...
<View
android:layout_width="match_parent"
android:layout_height="@dimen/activity_margin"></View>
</LinearLayout>
MainActivity.java: MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNoteEditText = (EditText) findViewById(R.id.notepad);
final MainActivity mainActivity = this;
mNoteEditText.setFocusable(false);
mNoteEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToastMessage("here", false);
mNoteEditText.setFocusable(true);
mNoteEditText.requestFocus();
mNoteEditText.setCursorVisible(true);
showKeyboard(mNoteEditText);
}
});
}
public void showKeyboard(EditText editText) {
InputMethodManager inputMethodManager =
(InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
The activity's manifest entry is: 该活动的清单条目为:
<activity
android:name=".ui.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan" >
<!--Main launcher.-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The problem is, the onClick method is firing (I can see the toast), but the other lines have no effect on the EditText (mNoteEditText). 问题是,onClick方法正在触发(我可以看到吐司),但是其他行对EditText(mNoteEditText)没有影响。
Thanks. 谢谢。
So here is the best solution of your problem. 因此,这是您问题的最佳解决方案。 Declare your manifest file like this- 这样声明清单文件-
<activity
android:name=".ui.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:windowSoftInputMode="stateHidden|adjustPan" >
<!--Main launcher.-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In your xml define editText like this 在您的xml中定义如下的editText
<EditText
android:id="@+id/notepad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:background="@android:color/transparent"
android:textSize="@dimen/font_large"
android:textStyle="normal"
android:textColor="@color/appPrimaryText"
android:textColorHint="@color/appAccentColour"
android:gravity="top"
android:cursorVisible="false"
android:scrollbars="vertical" />
In your mainactivity edit protected void onCreate(Bundle savedInstanceState) method like this- 在您的mainactivity中,编辑受保护的void onCreate(Bundle savedInstanceState)方法,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNoteEditText = (EditText) findViewById(R.id.notepad);
mNoteEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (view.getId() == mNoteEditText.getId())
{
mNoteEditText.setCursorVisible(true);
}
}
});
}
that's it :) 而已 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.