android - 如何通过文本观察器在 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;
}
解决方案
onTextChanged
当用户从 edittext 更改文本时调用。
et1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
t1.setText(et1.getText().toString());
}
});
推荐阅读
- powershell - 升级powershell会破坏它
- java - Java:如何在三元运算符中访问实例变量?
- python - 跨页面或等效的 Sphinx 脚注
- python - 如何在linux命令中间传递来自文本文件的参数
- html - 如何将 Navbar-Brand 居中在链接项的中间
- javascript - Javascript Uncaught TypeError:.split 不是函数
- sql-server - 显示第三个表中的所有记录,但在其他 2 个表中未找到的记录除外
- c# - 如何修复“Cudafy 在 CUDA 上出现错误 719”
- haskell - 非内射封闭类型族
- regex - 正则表达式匹配除以数字开头的字符串以外的所有内容