【问题标题】:Use addTextChangedListener without knowing EditText Id在不知道 EditText Id 的情况下使用 addTextChangedListener
【发布时间】:2021-01-11 17:13:06
【问题描述】:

我已将我的应用程序设置为从单独的 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);
    }

}

【问题讨论】:

    标签: java android xml addtextchangedlistener


    【解决方案1】:

    当您扩充一个新的TableRow 时,您可以为每个EditText 添加一个文本观察器。你只需要在你新创建的TableRow 中找到这些EditTexts 并为它们添加一个文本观察器。如果要在 XML 布局文件中添加文本观察器,可以使用 data binding libraryHere 是一个类似的问题。据我所知,除了数据绑定之外,您无法像添加 onClick 侦听器一样在 XML 布局中添加文本观察器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 2020-06-20
      相关资源
      最近更新 更多