【问题标题】:single edittext must call textwatcher or act as textview android单个edittext必须调用textwatcher或充当textview android
【发布时间】:2014-10-15 20:33:08
【问题描述】:

嗨,在我的应用程序中,如果 edittext 输入来自某种方法,并且如果 editText 的输入来自键盘,那么它必须显示 2 个 editText 值,那么它必须调用 textwatcher。

即:

  • 如果 edittext1 有来自虚拟键盘的输入,则触发 TextWatcher,它将调用方法并在 edittext2 中显示值[这里 edittext2 充当 textview]

  • 如果 edittext2 有来自虚拟键盘的输入,则触发 textwatcher,它将调用方法并在 edittext1 中显示结果[这里 edittext1 充当 textview]

这可能吗?如果是这样,请告诉我要遵循的概念。

谢谢

【问题讨论】:

    标签: android textwatcher


    【解决方案1】:

    直接实施可能会CRASH您的应用。您可以通过使用标志变量来解决问题

    public class MainActivity extends Activity {
    
        EditText et1, et2;
        String flag = "";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            et1 = (EditText) findViewById(R.id.editText1);
            et2 = (EditText) findViewById(R.id.editText2);
    
            // Here update the flag variable to keep track which text view is in use
            et1.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    flag = "ET1";
                }
            });
    
            // Here update the flag variable to keep track which text view is in use
            et2.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    flag = "ET2";
                }
            });
    
            et1.addTextChangedListener(new TextWatcher() {
    
                public void afterTextChanged(Editable s) {
                }
    
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                }
    
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                    // Set the value of editText2 value here if only flag contains
                    // ET1
                    // This will prevent changes if ET2 is being used
                    if (flag.equals("ET1")) {
                        et2.setText(s);
                    }
                }
            });
    
            et2.addTextChangedListener(new TextWatcher() {
    
                public void afterTextChanged(Editable s) {
                }
    
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                }
    
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                    // Set the value of editText1 value here only if flag equals ET2
                    if (flag.equals("ET2")) {
                        et1.setText(s);
                    }
                }
            });
        }
    }
    

    运行 在此处双击 EditText,然后再键入它们。您可以通过在适当的情况下启用和禁用编辑文本来进一步改进这一点,因此您只需单击一下即可实现相同的效果。

    如果您对代码有任何疑问,请在下方评论。

    【讨论】:

    • 谢谢,但不能使用复选框,因为必须减少击键次数...有没有办法检测到 edittext 的输入来自虚拟键盘?
    • 在这种情况下,TextWatcher 将为您提供帮助。你能解释一下number of keystroke must be reduced的意思吗?
    • 在我的应用程序中,必须只有 5 个 edittext 而没有其他小部件......不能在外部完成,所以当击键来自虚拟键时,然后调用 watcher 函数,否则充当 textview?
    • 这个怎么样。为两个edittext设置onClick侦听器,现在取决于单击哪个edittext,为某个变量设置一个值(类似于我使用复选框的方式),然后在将值设置为edittext之前检查变量的值并进行相应设置。
    • sorry dnt follow 你能解释一下吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多