如何解决在不知道 EditText Id 的情况下使用 addTextChangedListener
我已将我的应用程序设置为从单独的 XML 布局动态添加表格行,每行包含一个 ImageButton 以动态删除每一行。每行包含一个 TextView、两个 EditText 和 ImageButton。我在 ImageButton 的 XML 布局中使用了 android:onClick="onClick"
行,这样就无需尝试跟踪每个动态行的 Id。我现在面临一个新问题,在不知道任何 Id 的情况下,如何为每一行中的两个 TextEdit 实现一个 TextChangedListener?据我所知,XML 不包含 TextChangedListener,因此这可能必须完全在 Java 中完成。设置某种数组来保存每一行可能会起作用,但我想看看还能做什么。我的代码如下:
单独的 XML 布局(calculator_layout_table)
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/newRow">
<EditText
android:id="@+id/category_name3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:ems="10"
android:inputType="textPersonName"
android:text="Category Name" />
<EditText
android:id="@+id/editTextNumber3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:ems="10"
android:hint="@string/enter_a_percent"
android:inputType="number"
android:textAlignment="center" />
<TextView
android:id="@+id/result3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:text="$0"
android:textAlignment="center" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="4"
android:backgroundTint="#00FFFFFF"
android:scaleX=".7"
android:scaleY=".7"
android:src="@android:drawable/btn_dialog"
android:onClick="onClick"/>
</TableRow>
MainActivity (Java)
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onClick(View v) {
View row = (View) v.getParent();
ViewGroup container = ((ViewGroup)row.getParent());
container.removeView(row);
container.invalidate();
}
}
SecondFragment (Java)
public class SecondFragment extends Fragment {
@Override
public View onCreateView(
LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState
) {
return inflater.inflate(R.layout.fragment_second,container,false);
}
public void onViewCreated(@NonNull View view,Bundle savedInstanceState) {
super.onViewCreated(view,savedInstanceState);
view.findViewById(R.id.add_row_button).setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
createRow();
}
});
}
private void createRow(){
TableLayout bottomTable = getActivity().findViewById (R.id.bottomTable);
TableRow newRow = (TableRow) getLayoutInflater().inflate (R.layout.calculator_table_row,null,false);
bottomTable.addView(newRow);
}
}
解决方法
当您为新的 EditText
膨胀时,您可以为每个 TableRow
添加一个文本观察器。您只需要在新创建的 EditText
中找到这些 TableRow
并为它们添加文本观察器。如果要在 XML 布局文件中添加文本观察器,可以使用 data binding library。 Here 是一个类似的问题。据我所知,除了数据绑定之外,您无法像添加 onClick
侦听器那样在 XML 布局中添加文本观察器。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。