【问题标题】:to add textview and button in each row of android listview在 android listview 的每一行中添加 textview 和 button
【发布时间】:2013-01-24 15:11:23
【问题描述】:

在 android listview 的每一行中添加 textview 和 button 并且如果单击一行中的按钮,则应该只单击其行的 textview已编辑或更改,其他行必须保持不受影响

【问题讨论】:

    标签: android listview button textview


    【解决方案1】:

    要获得它,您需要使用自定义适配器和自定义布局。

    在您的行 XML 文件中:

    <TextView
        android:id="@+id/label"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />
    

    在你的基础适配器中,你可以设置监听器做一些事情:

    public class MyBaseAdapter extends BaseAdapter {
    
        // Some other method implementation here...
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Initialize the convertView here...
    
            LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.row_layout);
            Button button = (Button) convertView.findViewById(R.id.button);
    
            layout.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Toast.makeText(context, "Row clicked!", Toast.LENGTH_LONG).show();
                }
            });
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Toast.makeText(context, "Button clicked!", Toast.LENGTH_LONG).show();
                }
            });
        }    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-01
      相关资源
      最近更新 更多