【问题标题】:How to add same onClickListener in every textView in android dialog?如何在android对话框的每个textView中添加相同的onClickListener?
【发布时间】:2015-03-08 23:05:23
【问题描述】:

我有一个自定义的 android 对话框,它有几个 textView。每个文本视图都显示不同的文本。目的是当用户单击文本视图时,应关闭对话框并将该文本视图的 textColor 应返回给父级。

这是我的对话框布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff000000">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="White"
    android:id="@+id/textView_white"
    android:layout_gravity="center_horizontal"
    android:textSize="30dp"
    android:textIsSelectable="true"
    android:clickable="true"
    android:gravity="center"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:textColor="#fffffbfd" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Black"
    android:id="@+id/textView_black"
    android:layout_gravity="center_horizontal"
    android:textSize="30dp"
    android:textIsSelectable="true"
    android:clickable="true"
    android:gravity="center"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:textColor="#ffffffff"
    android:password="false"
    android:background="#ff000000" />
</LinearLayout>

我正在从父活动以这种方式启动对话框:

        final Context context = MyWidgetConfigureActivity.this;
        final Dialog dialog = new Dialog(context);

        dialog.setContentView(R.layout.color_chooser);
        dialog.setTitle("Choose Text Color");

        dialog.show();

我想为对话框内的每个 textView 添加相同的 onClickListener。如何实现这一目标?对话框中有 20 多个 textView,我不想手动将 onClickListener 添加到每个 textView?有没有更好的方法来做同样的事情?

【问题讨论】:

    标签: android


    【解决方案1】:

    我有两种方式,首先我会为每个TextView设置一个id,然后使用dialog.findViewById(id)的方法来获取每个TextView,并设置一个onClickListener,但我觉得这种方式很伤脑筋。所以第二种方式是:先给对话框布局根视图一个id,像这样

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/dialog_root"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff000000">
    

    那么java代码是:

    LinearLayout rootLayout = (LinearLayout) dialog.findViewById(R.id.dialog_root);
    for (int i = 0; i < rootLayout.getChildCount(); i++) {
        TextView tv = (TextView) rootLayout.getChildAt(i);
        tv.setOnClickListener(this);
    }
    

    ... onClick 方法是这样的。

    @Override
    public void onClick(View v) {
        int color = ((TextView) v).getCurrentTextColor();
        // TODO ...
    }
    

    【讨论】:

    • 这工作正常 :-) 谢谢,请告诉我单击项目后如何关闭对话框?
    • 在onClick中使用dialog.dismiss()
    【解决方案2】:
    OnClickListener click = new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    Toast.makeText(this,"You can use", Toast.LENGTH_LONG).show();
                }
            };
    
    TextView t1 = (TextView)findViewById(id);
    TextView t2 = (TextView)findViewById(id);
    TextView t3 = (TextView)findViewById(id);
    
    t1.setOnClickListener(click);
    t2.setOnClickListener(click);
    t3.setOnClickListener(click);
    

    【讨论】:

      【解决方案3】:

      我建议改变你的方法。如果您有 20 多个可能看起来相同的 TextView,为什么不使用 ListView?看看here

      【讨论】:

      • Hi Mighter,每个 textView 都有不同的颜色。它们不一样。
      • 为什么不能在listView中设置颜色?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多