温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

TextWatcher如何找到调用它的EditText

发布时间:2020-06-22 14:16:03 来源:网络 阅读:4468 作者:wy521angel 栏目:移动开发

    和我在南方一起工作的朋友说,“南北方的差异其实蛮大的。”我家在北方,也在南方工作,不过我倒是觉得差异不怎么大,因为我在北方的时候,就没有女朋友,而来到了南方,同样没有女朋友。

    开发时遇到一个问题,如同标题,当一个类继承了TextWatcher时,倘若这个类中有很多EditText控件,那么如何知道调用TextWatcher的是哪一个EditText控件呢?如果一个类继承的是OnClickListener,那可以通过View获取控件的Id值,从而分辨控件,做对应操作。可惜TextWatcher似乎没有类似的方法。我是这样解决的:

    布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:padding="10dp">

    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/edittext_1" />
    <EditText
        android:id="@+id/edit2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/edittext_2" />
    <EditText
        android:id="@+id/edit3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/edittext_3" />
    <EditText
        android:id="@+id/edit4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/edittext_4" />
    <EditText
        android:id="@+id/edit5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/edittext_5" />

</LinearLayout>

    很简单,只是写了几个EditText控件。

    主类:

package com.example.edittexttest;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EditTextTest extends Activity {

	EditText edit1, edit2, edit3, edit4, edit5;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_text_test);

		edit1 = (EditText) findViewById(R.id.edit1);
		edit1.addTextChangedListener(new ClassOfTextWatcher(edit1));
		setCursorToEnd(edit1);
		edit2 = (EditText) findViewById(R.id.edit2);
		edit2.addTextChangedListener(new ClassOfTextWatcher(edit2));
		setCursorToEnd(edit2);
		edit3 = (EditText) findViewById(R.id.edit3);
		edit3.addTextChangedListener(new ClassOfTextWatcher(edit3));
		setCursorToEnd(edit3);
		edit4 = (EditText) findViewById(R.id.edit4);
		edit4.addTextChangedListener(new ClassOfTextWatcher(edit4));
		setCursorToEnd(edit4);
		edit5 = (EditText) findViewById(R.id.edit5);
		edit5.addTextChangedListener(new ClassOfTextWatcher(edit5));
		setCursorToEnd(edit5);

	}

	private class ClassOfTextWatcher implements TextWatcher {

		private TextView view;

		public ClassOfTextWatcher(View view) {
			
			if (view instanceof TextView)
				this.view = (TextView) view;
			else
				throw new ClassCastException(
						"view must be an instance Of TextView");
		}

		@Override
		public void afterTextChanged(Editable s) {
			
			if (s.length() <= 0) {
				switch (view.getId()) {

				case R.id.edit1:
					Toast.makeText(EditTextTest.this, "第一个编辑框为空!",
							Toast.LENGTH_LONG).show();
					break;
				case R.id.edit2:
					Toast.makeText(EditTextTest.this, "第二个编辑框为空!",
							Toast.LENGTH_LONG).show();
					break;
				case R.id.edit3:
					Toast.makeText(EditTextTest.this, "第三个编辑框为空!",
							Toast.LENGTH_LONG).show();
					break;
				case R.id.edit4:
					Toast.makeText(EditTextTest.this, "第四个编辑框为空!",
							Toast.LENGTH_LONG).show();
					break;
				case R.id.edit5:
					Toast.makeText(EditTextTest.this, "第五个编辑框为空!",
							Toast.LENGTH_LONG).show();
					break;
				default:
					break;
				}
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

		}

	}
	
	//将编辑框的光标移动到末尾
	public void setCursorToEnd(EditText text){
		String content = text.getText().toString();
		text.setSelection(content.length());
	}
}

    写了一个叫“ClassOfTextWatcher”的内部类,它实现了TextWatcher接口,这个内部类的构造方法中传入View控件来获取控件的Id。

    效果图:

TextWatcher如何找到调用它的EditText

TextWatcher如何找到调用它的EditText


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI