首页 > 解决方案 > 如何通过文本观察器在 android 中设置文本

问题描述

我最近创建了一个具有文本视图和编辑文本的类。我正在使用编辑文本来获取用户的一些输入,并且我希望我的文本视图显示用户输入的内容。我需要提到我的标签是动态创建的,我不确定如何引用他们。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;
    }

在此处输入图像描述

标签: androiddynamic

解决方案


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

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

推荐阅读