【问题标题】:How to set text in android, through a text watcher如何通过文本观察器在 android 中设置文本
【发布时间】:2020-05-18 10:31:45
【问题描述】:

我最近创建了一个具有文本视图和编辑文本的类。 我正在使用编辑文本来获取用户的一些输入,并且我希望我的文本视图显示用户输入的内容。 我需要提到我的标签是动态创建的,我不知道如何引用它们。 C0你能给我一个线索吗?

public TextView itemName(Context context){
        final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final TextView itemName = new EditText(context);
        itemName.setLayoutParams(lparams);

        return itemName;
    }

    public EditText desiredQuantity(Context context) {

        final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        final EditText desiredQuantity = new EditText(context);
        desiredQuantity.setLayoutParams(lparams);
        desiredQuantity.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

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

                itemName.setText(desiredQuantity.getText().toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        return desiredQuantity;
    }

enter image description here

【问题讨论】:

  • 只需将 settext 放入 onTextChanged
  • 请直接在您的问题中显示代码,而不是屏幕截图。这将有助于我们更好地回答您的问题并分析代码。以此为例:meta.stackexchange.com/help/formatting

标签: android dynamic


【解决方案1】:

onTextChanged 当用户从 edittext 更改文本时调用。

et1.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        t1.setText(et1.getText().toString());
    }
});

【讨论】:

  • 它告诉我无法解析符号。我应该提一下,文本视图是通过类中的代码生成的
猜你喜欢
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 2016-11-05
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多