背景
假设您有多个EditText实例。
您希望能够使用键盘的下一个按钮(用作ENTER键的替换(在它们之间切换。
每个EditText可能有一个可以显示在多行中的长内容(假设我希望将其限制为3行,如果文本仍然太长,请使用省略号(。
问题
正如我所注意到的,TextView和EditText都有非常奇怪的行为,并且缺乏一些基本功能。其中之一是,如果您希望转到下一个视图,则需要为每个EditText实例都有一个singleLine="true"。
我尝试过的
我已经尝试了下一个xml布局(以及其他尝试(,但它不起作用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"
android:gravity="center" tools:context=".MainActivity">
<EditText android:id="@+id/editText1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:nextFocusDown="@+id/editText2"
android:singleLine="false" android:imeOptions="actionNext"
android:maxLines="3" android:ellipsize="end"
android:text="@string/very_long_text" />
<EditText android:id="@+id/editText2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:nextFocusDown="@+id/editText3"
android:singleLine="false" android:imeOptions="actionNext"
android:maxLines="3" android:ellipsize="end"
android:text="@string/very_long_text" />
<EditText android:id="@+id/editText3" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:singleLine="false"
android:maxLines="3" android:ellipsize="end"
android:text="@string/very_long_text"/>
</LinearLayout>
我也尝试了下一个代码,但这真的是一个愚蠢的解决方案:
...
final EditText editText=(EditText)findViewById(R.id.editText1);
editText.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(final TextView v,final int actionId,final KeyEvent event)
{
if(actionId==EditorInfo.IME_NULL)
{
final View view=findViewById(editText.getNextFocusDownId());
if(view!=null)
{
view.requestFocus();
return true;
}
}
return false;
}
});
它是有效的,但这是一个愚蠢的解决方案,因为接下来的原因:
- 我需要为每个EditText设置这种行为(或者扩展EditText并在那里添加某种逻辑(
- 软键盘上的键不会显示"下一个"。相反,它显示ENTER键
- 玩XML不允许我在最后设置省略号,我一直在滚动
问题
有更好的方法来实现这一点吗?一个优雅、有效并显示"下一个"键而不是ENTER键的?
我发现这个问题与"完成多行编辑文本"操作按钮帖子类似。既然我已经写了答案,让我再写一遍。不过,我只能回答你的前2点,因为我还没有使用椭圆(我认为一旦你用下一个按钮设置,应该相对容易(。
以下Java代码可用于将键盘输入更改为"完成"/"下一步"按钮:
////////////Code to Hide SoftKeyboard on Enter (DONE) Press///////////////
editText1.setRawInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
editText1.setImeActionLabel("DONE",EditorInfo.IME_ACTION_DONE); //Set Return Carriage as "DONE"
editText1.setImeOptions(EditorInfo.IME_ACTION_DONE);
editText1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (event == null) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Capture soft enters in a singleLine EditText that is the last EditText
// This one is useful for the new list case, when there are no existing ListItems
editText1.clearFocus();
editText2.requestFocus(); //Call the next Edit_Text into Focus
//Comment above requestFocus() for edit_text3
//Hiding SoftKeyboard. Uncomment it for edit_text3
//InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
//inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}
else if (actionId == EditorInfo.IME_ACTION_NEXT) {
// Capture soft enters in other singleLine EditTexts
} else if (actionId == EditorInfo.IME_ACTION_GO) {
} else {
// Let the system handle all other null KeyEvents
return false;
}
}
else if (actionId == EditorInfo.IME_NULL) {
// Capture most soft enters in multi-line EditTexts and all hard enters;
// They supply a zero actionId and a valid keyEvent rather than
// a non-zero actionId and a null event like the previous cases.
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// We capture the event when the key is first pressed.
} else {
// We consume the event when the key is released.
return true;
}
}
else {
// We let the system handle it when the listener is triggered by something that
// wasn't an enter.
return false;
}
return true;
}
});
这里有答案-如果需要,可以同时设置它们。然而,强制它实际显示下一个,并让回车键充当下一个键取决于键盘。我知道在Swype中,我们故意覆盖任何多行的文本字段,以始终显示回车键并充当换行符,而从不显示下一个键。没有办法强迫它像你想要的那样在所有键盘上工作。