Android Edittext 清空按鈕功能的實現

1. 佈局xml(僅貼出輸入框部分)

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5"
        android:gravity="bottom" >

        <TextView
            android:id="@+id/txv_username"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:layout_weight="1.5"
            android:text="@string/user_name"
            android:textSize="@dimen/BigLabelFontSize" />

        <EditText
            android:id="@+id/edt_username"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:layout_weight="4"
            android:background="@drawable/edit_bg_login"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:singleLine="true" >
        </EditText>

        <Button
            android:id="@+id/button_name_clear"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/delete"
            android:visibility="invisible" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <View
            android:layout_width="10dp"
            android:layout_height="0.5dp"
            android:background="#FFFFFF" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="0.5dp"
            android:background="#D4D4D4" />

        <View
            android:layout_width="10dp"
            android:layout_height="0.5dp"
            android:background="#FFFFFF" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5"
        android:gravity="bottom" >

        <TextView
            android:id="@+id/txv_password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:layout_weight="1.5"
            android:text="@string/pass_word"
            android:textSize="@dimen/BigLabelFontSize" />

        <EditText
            android:id="@+id/edt_password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:layout_weight="4"
            android:background="@drawable/edit_bg_login"
            android:inputType="textPassword"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:singleLine="true" >
        </EditText>

        <Button
            android:id="@+id/button_psw_clear"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/delete"
            android:visibility="invisible" />
    </LinearLayout>


2. 代碼實現(僅貼出實現清除按鈕功能部分代碼)

        // 清空用戶名按鈕
	private Button clearNameButton = null;
	// 清空密碼按鈕
	private Button clearPswButton = null;
	// 用戶名輸入框
	private EditText edtUserCode = null;
	// 密碼輸入框
	private EditText edtPassWord = null;


                edtUserCode = (EditText) this.findViewById(R.id.edt_username);
		edtPassWord = (EditText) this.findViewById(R.id.edt_password);
		edtUserCode.setSelection(edtUserCode.getText().length());
		edtPassWord.setSelection(edtPassWord.getText().length());
		clearNameButton = (Button) findViewById(R.id.button_name_clear);
		clearPswButton = (Button) findViewById(R.id.button_psw_clear);		
		/**
		 * 用戶名輸入框監聽事件
		 */
		edtUserCode.addTextChangedListener(new TextWatcher() {

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				if (edtUserCode.getText().toString() != null
						&& !edtUserCode.getText().toString().equals("")) {
					clearNameButton.setVisibility(View.VISIBLE);
				} else {
					clearNameButton.setVisibility(View.INVISIBLE);
				}
			}

		});

		/**
		 * 密碼輸入框監聽事件
		 */
		edtPassWord.addTextChangedListener(new TextWatcher() {

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				if (edtPassWord.getText().toString() != null
						&& !edtPassWord.getText().toString().equals("")) {
					clearPswButton.setVisibility(View.VISIBLE);
				} else {
					clearPswButton.setVisibility(View.INVISIBLE);
				}
			}

		});

		/**
		 * 清空用戶名按鈕的監聽事件
		 */
		clearNameButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				edtUserCode.setText("");
			}
		});

		/**
		 * 清空密碼按鈕的監聽事件
		 */
		clearPswButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				edtPassWord.setText("");
			}
		});




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章