public class LockActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.lockscreen); initUI(); } public EditText editText,editText2,editText3,editText4; private void initUI(){ editText = (EditText) findViewById(R.id.editText1); editText2 = (EditText) findViewById(R.id.editText2); editText3 = (EditText) findViewById(R.id.editText3); editText4 = (EditText) findViewById(R.id.editText4); editText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // 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 afterTextChanged(Editable s) { // TODO Auto-generated method stub String resString =editText.getText().toString().trim(); if (resString!=null && resString.length()>0) { editText2.requestFocus(); } } }); editText2.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // 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 afterTextChanged(Editable s) { // TODO Auto-generated method stub String resString =editText2.getText().toString().trim(); if (resString!=null && resString.length()>0) { editText3.requestFocus(); } } }); editText3.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // 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 afterTextChanged(Editable s) { // TODO Auto-generated method stub String resString =editText3.getText().toString().trim(); if (resString!=null && resString.length()>0) { editText4.requestFocus(); } } }); editText4.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // 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 afterTextChanged(Editable s) { // TODO Auto-generated method stub String resString =editText4.getText().toString().trim(); if (resString!=null && resString.length()>0) { //隐藏键盘 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText4.getWindowToken(), 0); //强制隐藏键盘 } } }); } }
布局如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingRight="20dp"> <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_weight="1" android:layout_marginLeft="20dp" android:layout_height="wrap_content" android:singleLine="true" android:ems="1" android:inputType="textPassword" > </EditText> <EditText android:id="@+id/editText2" android:layout_width="fill_parent" android:layout_weight="1" android:layout_marginLeft="20dp" android:layout_height="wrap_content" android:singleLine="true" android:ems="1" android:inputType="textPassword" > </EditText> <EditText android:id="@+id/editText3" android:layout_width="fill_parent" android:layout_weight="1" android:layout_marginLeft="20dp" android:layout_height="wrap_content" android:singleLine="true" android:ems="1" android:inputType="textPassword" > </EditText> <EditText android:id="@+id/editText4" android:layout_width="fill_parent" android:layout_weight="1" android:layout_marginLeft="20dp" android:layout_height="wrap_content" android:singleLine="true" android:ems="1" android:inputType="textPassword" > </EditText> </LinearLayout> </RelativeLayout>
原文地址:http://blog.csdn.net/jgszhuzhu/article/details/39325669