[英]Use addTextChangedListener without knowing EditText Id
I have set up my app to dynamically add table rows from a separate XML layout, each row containing an ImageButton to delete each row dynamically.我已将我的应用程序设置为从单独的 XML 布局动态添加表行,每行包含一个 ImageButton 以动态删除每一行。 Each row contains a TextView, two EditText's, and the ImageButton.每行包含一个 TextView、两个 EditText 和 ImageButton。 I used the line android:onClick="onClick"
in the XML layout for the ImageButton, which eliminated the need to try to keep track of the Id for each dynamic row.我在 ImageButton 的 XML 布局中使用了android:onClick="onClick"
行,这样就无需尝试跟踪每个动态行的 Id。 I am now facing a new problem, without knowing any Id's, how can I implement a TextChangedListener for both of the TextEdit's in each row?我现在面临一个新问题,在不知道任何 Id 的情况下,如何为每行中的两个 TextEdit 实现 TextChangedListener? To my knowledge, XML does not contain a TextChangedListener, so this will likely have to be done completely in Java.据我所知,XML 不包含 TextChangedListener,因此这可能必须在 Java 中完全完成。 Setting up some kind of array to hold each row might work, but I wanted to see what else could be done.设置某种数组来保存每一行可能会起作用,但我想看看还能做什么。 My code is below:我的代码如下:
Separate XML layout (calculator_layout_table)单独的 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)主活动 (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)第二片段(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);
}
}
You could add a text watcher for each EditText
when you inflate a new TableRow
.当您为新的TableRow
充气时,您可以为每个EditText
添加一个文本观察器。 You just need to find these EditText
s in your newly created TableRow
and add a text watcher for them.您只需要在新创建的TableRow
中找到这些EditText
并为它们添加一个文本观察器。 If you want to add a text watcher in the XML layout file, you could use data binding library .如果要在 XML 布局文件中添加文本观察器,可以使用数据绑定库。 Here is an analogous question. 这是一个类似的问题。 As far as I know, there is no way you can add a text watcher in the XML layout in the same way you added the onClick
listener except for data binding.据我所知,除了数据绑定之外,您无法像添加onClick
侦听器一样在 XML 布局中添加文本观察器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.