android 多个edittext的输入和删除

来源:互联网 发布:mac无法显示移动硬盘 编辑:程序博客网 时间:2024/05/29 16:58

在一个老同事的帮助下完成,感谢。页面部分:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"             android:layout_width="fill_parent" android:layout_height="fill_parent"             android:background="@drawable/background">    <include layout="@layout/top_org_info"/>    <include layout="@layout/own_pieces_process"/>    <TableRow android:layout_width="fill_parent" android:gravity="center">        <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content">            <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent"                         android:gravity="center" android:layout_marginBottom="100dip">                <TableRow android:layout_width="fill_parent">                    <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content"                                 android:gravity="center">                        <TableRow>                            <!--<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"                                          android:gravity="center">                                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"                                          android:text="密码:" style="@style/title_text"/>                                <EditText                                        android:id="@+id/own_pieces_captcha"                                        android:layout_width="360dp"                                        android:layout_height="wrap_content"                                        android:numeric="integer"                                        android:maxLines="1"                                        android:singleLine="true"                                        android:maxLength="6"/>                            </LinearLayout>-->                            <LinearLayout                                    android:layout_width="wrap_content"                                    android:layout_height="wrap_content"                                    android:gravity="center">                                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"                                          android:text="六位取货密码:" style="@style/title_text"/>                                <EditText                                        android:id="@+id/own_pieces_captcha_one"                                        android:layout_width="43dp"                                        android:layout_height="wrap_content"                                        android:background="@color/white"                                        android:cursorVisible="false"                                        android:ems="1"                                        android:gravity="center"                                        android:lines="1"                                        android:numeric="integer"                                        android:maxLines="1"                                        android:singleLine="true"                                        android:maxLength="1"                                        android:textSize="31sp" >                                </EditText>                                <EditText                                        android:id="@+id/own_pieces_captcha_two"                                        android:layout_width="43dp"                                        android:layout_height="wrap_content"                                        android:layout_marginLeft="20dp"                                        android:background="@color/white"                                        android:cursorVisible="false"                                        android:ems="1"                                        android:numeric="integer"                                        android:maxLines="1"                                        android:singleLine="true"                                        android:maxLength="1"                                        android:gravity="center"                                        android:textSize="31sp" />                                <EditText                                        android:id="@+id/own_pieces_captcha_three"                                        android:layout_width="42dp"                                        android:layout_height="wrap_content"                                        android:layout_marginLeft="20dp"                                        android:background="@color/white"                                        android:cursorVisible="false"                                        android:ems="1"                                        android:numeric="integer"                                        android:maxLines="1"                                        android:singleLine="true"                                        android:maxLength="1"                                        android:gravity="center"                                        android:textSize="31sp" />                                <EditText                                        android:id="@+id/own_pieces_captcha_four"                                        android:layout_width="43dp"                                        android:layout_height="wrap_content"                                        android:layout_marginLeft="20dp"                                        android:background="@color/white"                                        android:cursorVisible="false"                                        android:ems="1"                                        android:numeric="integer"                                        android:maxLines="1"                                        android:singleLine="true"                                        android:maxLength="1"                                        android:gravity="center"                                        android:textSize="31sp" />                                <EditText                                        android:id="@+id/own_pieces_captcha_five"                                        android:layout_width="43dp"                                        android:layout_height="wrap_content"                                        android:layout_marginLeft="20dp"                                        android:background="@color/white"                                        android:cursorVisible="false"                                        android:ems="1"                                        android:numeric="integer"                                        android:maxLines="1"                                        android:singleLine="true"                                        android:maxLength="1"                                        android:gravity="center"                                        android:textSize="31sp" />                                <EditText                                        android:id="@+id/own_pieces_captcha_six"                                        android:layout_width="43dp"                                        android:layout_height="wrap_content"                                        android:layout_marginLeft="20dp"                                        android:background="@color/white"                                        android:cursorVisible="false"                                        android:ems="1"                                        android:numeric="integer"                                        android:maxLines="1"                                        android:singleLine="true"                                        android:maxLength="1"                                        android:gravity="center"                                        android:textSize="31sp" />                                <EditText                                        android:id="@+id/own_pieces_captcha"                                        android:layout_width="30dp"                                        android:layout_height="wrap_content"                                        android:visibility="gone" />                            </LinearLayout>                        </TableRow>                    </TableLayout>                </TableRow>            </TableLayout>        </RelativeLayout>    </TableRow>    <View style="@style/background_split_blue"/>    <TextView android:layout_height="15dp"/></TableLayout>
后台处理:

package com.ourangel.terminal.activity;import android.content.Intent;import android.os.Bundle;import android.text.*;import android.view.KeyEvent;import android.view.View;import android.widget.EditText;import android.widget.TextView;import com.ourangel.terminal.R;import com.ourangel.terminal.application.CustomApplication;/** * Created by xuwenqiang on 14-5-5. */public class OwnPiecesActivity extends CommonActivity implements TextWatcher, View.OnClickListener, View.OnFocusChangeListener, View.OnKeyListener {    // 验证码文本输入框    private EditText captchaEditText, captchaEtOne, captchaEtTwo, captchaEtThree, captchaEtFour, captchaEtFive, captchaEtSix;    // 手机后四位文本输入框    public static OwnPiecesActivity instance;    private int editInt;    @Override    public void onCreate(Bundle savedInstanceState) {        instance = this;        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_own_pieces);        CustomApplication.getInstance().addActivity(this);        findView();        initWidget();    }    void findView() {        captchaEtOne = (EditText) findViewById(R.id.own_pieces_captcha_one);        captchaEtTwo = (EditText) findViewById(R.id.own_pieces_captcha_two);        captchaEtThree = (EditText) findViewById(R.id.own_pieces_captcha_three);        captchaEtFour = (EditText) findViewById(R.id.own_pieces_captcha_four);        captchaEtFive = (EditText) findViewById(R.id.own_pieces_captcha_five);        captchaEtSix = (EditText) findViewById(R.id.own_pieces_captcha_six);        captchaEditText = (EditText) findViewById(R.id.own_pieces_captcha);        captchaEtOne.addTextChangedListener(this);        captchaEtTwo.addTextChangedListener(this);        captchaEtThree.addTextChangedListener(this);        captchaEtFour.addTextChangedListener(this);        captchaEtFive.addTextChangedListener(this);        captchaEtSix.addTextChangedListener(this);        captchaEtOne.setOnKeyListener(this);        captchaEtTwo.setOnKeyListener(this);        captchaEtThree.setOnKeyListener(this);        captchaEtFour.setOnKeyListener(this);        captchaEtFive.setOnKeyListener(this);        captchaEtSix.setOnKeyListener(this);        captchaEtOne.setOnFocusChangeListener(this);        captchaEtTwo.setOnFocusChangeListener(this);        captchaEtThree.setOnFocusChangeListener(this);        captchaEtFour.setOnFocusChangeListener(this);        captchaEtFive.setOnFocusChangeListener(this);        captchaEtSix.setOnFocusChangeListener(this);    }    void initWidget() {        displayOperateTime = (TextView) findViewById(R.id.textView_displayOperateTime);        super.onDisplayOperateTime(this);    }    @Override    public void beforeTextChanged(CharSequence s, int start, int count,                                  int after) {    }    @Override    public void onTextChanged(CharSequence s, int start, int before, int count) {    }    @Override    public void afterTextChanged(Editable s) {        switch (editInt) {            case 1:                if (s.toString().length() == 1) {                    captchaEtTwo.setFocusable(true);                    captchaEtTwo.requestFocus();                }                break;            case 2:                if (s.toString().length() == 1) {                    captchaEtThree.setFocusable(true);                    captchaEtThree.requestFocus();                }                break;            case 3:                if (s.toString().length() == 1) {                    captchaEtFour.setFocusable(true);                    captchaEtFour.requestFocus();                }                break;            case 4:                if (s.toString().length() == 1) {                    captchaEtFive.setFocusable(true);                    captchaEtFive.requestFocus();                }                break;            case 5:                if (s.toString().length() == 1) {                    captchaEtSix.setFocusable(true);                    captchaEtSix.requestFocus();                }                break;            case 6:                break;            default:                break;        }    }    @Override    public void onClick(View v) {    }    @Override    public void onFocusChange(View v, boolean hasFocus) {        switch (v.getId()) {            case R.id.own_pieces_captcha_one:                if (hasFocus) {                    editInt = 1;                    captchaEtOne.setSelection(captchaEtOne.getText().toString().length());                }                break;            case R.id.own_pieces_captcha_two:                if (hasFocus) {                    editInt = 2;                    captchaEtTwo.setSelection(captchaEtTwo.getText().toString().length());                }                break;            case R.id.own_pieces_captcha_three:                if (hasFocus) {                    editInt = 3;                    captchaEtThree.setSelection(captchaEtThree.getText().toString().length());                }                break;            case R.id.own_pieces_captcha_four:                if (hasFocus) {                    editInt = 4;                    captchaEtFour.setSelection(captchaEtFour.getText().toString().length());                }                break;            case R.id.own_pieces_captcha_five:                if (hasFocus) {                    editInt = 5;                    captchaEtFive.setSelection(captchaEtFive.getText().toString().length());                }                break;            case R.id.own_pieces_captcha_six:                if (hasFocus) {                    editInt = 6;                    captchaEtSix.setSelection(captchaEtSix.getText().toString().length());                }                break;            default:                break;        }    }    @SuppressWarnings("static-access")    @Override    public boolean onKey(View v, int keyCode, KeyEvent event) {        if (KeyEvent.KEYCODE_DEL == event.getKeyCode() && event.getAction() == event.ACTION_DOWN)            switch (editInt) {                case 1:                    break;                case 2:                    captchaEtOne.setFocusable(true);                    captchaEtOne.requestFocus();                    break;                case 3:                    captchaEtTwo.setFocusable(true);                    captchaEtTwo.requestFocus();                    break;                case 4:                    captchaEtThree.setFocusable(true);                    captchaEtThree.requestFocus();                    break;                case 5:                    captchaEtFour.setFocusable(true);                    captchaEtFour.requestFocus();                    break;                case 6:                    captchaEtFive.setFocusable(true);                    captchaEtFive.requestFocus();                    break;                default:                    break;            }        return false;    }}


0 0