我的一项活动中有一个 EditText 对象。我已经实现了在键盘上插入“完成”按钮的代码,并在用户完成输入时隐藏它。不过我注意到,当文本达到我附加到 EditText 对象的最大长度时,它不会自动返回。在我的这个应用程序的 iPhone 版本中,我已经能够为我的文本框设置一个名为“自动返回”的属性,它会自动返回,这就是我想为 Android 版本实现的,但我没有知识。这是我写的代码:
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:maxLength="10"
android:hint="@string/hint"
android:inputType="textImeMultiLine"
android:imeOptions="actionDone"
android:layout_marginTop="15dp"
android:gravity="center"/>
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_type_order);
getActionBar().setDisplayHomeAsUpEnabled(true);
editText1 = (EditText)findViewById(R.id.editText1);
editText1.setLines(10);
editText1.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
InputMethodManager keyboard1 = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
keyboard1.hideSoftInputFromInputMethod(editText1.getWindowToken(), 0);
}
});