问题
In edittext, after typing 'Enter' key, system make a new line inside it. I'd like to focus on next edittext, no new line. how to code? my code in xml is below
<EditText
android:id="@+id/txtNPCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="@+id/lblNPCode"
android:layout_below="@+id/lblNPCode"
android:layout_centerHorizontal="true"/>
<EditText
android:id="@+id/txtCNPCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="@+id/lblCNPCode"
android:layout_below="@+id/lblCNPCode"
android:layout_centerHorizontal="true"/>
I also caputer key code in setOnKeyListener
tCNPCode.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == 66) {
Toast.makeText(S_PCode.this, "Enter Key", Toast.LENGTH_LONG).show();
//tNPCode.setFocusable(true);
}
return false;
}
});
回答1:
You have to use the requestFocus method. In your case it will be something like:
tCNPCode.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == 66) {
txtCNPCode.requestFocus();
}
return false;
}
});
回答2:
You don't even have to use OnKeyListener
. Simply add line android:singleLine="true"
to your EditText
. After this operation EditText
behaves exactly like you want. So, in your particular case:
<EditText
android:id="@+id/txtNPCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="@+id/lblNPCode"
android:layout_below="@+id/lblNPCode"
android:layout_centerHorizontal="true"
android:singleLine="true"
/>
<EditText
android:id="@+id/txtCNPCode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignLeft="@+id/lblCNPCode"
android:layout_below="@+id/lblCNPCode"
android:layout_centerHorizontal="true"
/>
solves the problem.
回答3:
The best way is to use one of the options for inputType, for example: android:inputType="textPersonName"
This will achieve the wanted effect. Check out the other options too, it'll save you some time in future :)
You shouldn't use android:singleLine="true", since it's deprecated. Here's what the documentations says about it:
This attribute is deprecated and is replaced by the textMultiLine flag in the inputType attribute. Use caution when altering existing layouts, as the default value of singeLine is false (multi-line mode), but if you specify any value for inputType, the default is single-line mode. (If both singleLine and inputType attributes are found, the inputType flags will override the value of singleLine.).
回答4:
Consider IME options, good description here http://androidcookbook.com/Recipe.seam;jsessionid=C824437B48F346E23FBE5828969029B4?recipeId=422 and documentation here http://developer.android.com/reference/android/widget/TextView.html#attr_android:imeOptions.
The only thing you need is add attribute android:imeOptions="actionNext" on each EditText. Keyboard appears with a Next key where Enter used to be and allows to navigate to the next EditText.
But order of "actionNext" is from top to bottom. For example, if you have horizontal layout:
<LinearLayout android:orientation="horizontal" >
<EditText android:id="@+id/et1" android:imeOptions="actionNext" />
<EditText android:id="@+id/et2" android:imeOptions="actionNext" />
</LinearLayout>
et2 never get focus. You should fix it in the code:
et1.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event)
{
switch (actionId)
{
case EditorInfo.IME_ACTION_NEXT:
boolean result = false;
TextView v1 = (TextView) v.focusSearch(View.FOCUS_RIGHT);
if (v1 != null)
result = v1.requestFocus(View.FOCUS_RIGHT);
else if (!result)
{
v1 = (TextView) v.focusSearch(View.FOCUS_DOWN);
if (v1 != null)
result = v1.requestFocus(View.FOCUS_DOWN);
}
if (!result)
v.onEditorAction(actionId);
break;
default:
v.onEditorAction(actionId);
break;
}
return true;
}
});
Original link: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/OykOG6XtE8w
回答5:
Just set the maximum number of line to the EditText
box in xml layout file:
android:maxLength="1"
回答6:
Use Single Line of Code
tCNPCode.setSingleLine(true);
It will navigate to next item on enter press
来源:https://stackoverflow.com/questions/3019965/keycode-enter-to-next-edittext