在android中,如何在布局的多个组件中拥有一个唯一的按钮?
我有一个xml,它由一个线性布局组成,包含一个按钮和一个文本视图,如下所示:在android中,如何在布局的多个组件中拥有一个唯一的按钮?,android,android-layout,android-studio,layout,Android,Android Layout,Android Studio,Layout,我有一个xml,它由一个线性布局组成,包含一个按钮和一个文本视图,如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" android:orientation="ve
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<Button
android:id="@+id/btnCell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:paddingLeft="40dp"
android:text="Button"
android:textColor="@color/blueText" />
<View
android:height="wrap_content"
android:width="wrap_content"
android:background="@drawable/Test"/>
</LinearLayout>
btnCell = (Button) thirdView.findViewById(R.id.btnCell);
btnCell.setOnClickListener(this);
并实现了侦听器
@Override
public void onClick(View v) {
if (v == btnCell) {
System.out.println("entered if");
}
System.out.println("entered function");
}
问题是,当我在显示第一个视图和隐藏第二个视图时单击按钮时,按钮工作正常,但当我取消隐藏第二个布局、隐藏第一个布局并继续单击按钮时,应该与第一个相同,但在不同的布局中,什么也不会发生。我搜索并发现,之所以会出现这种情况,是因为由于视图层次结构,id仅分配给第一个布局中显示的按钮,而不是第二个布局中的按钮。如何使两个按钮对同一动作做出反应,而不在每个布局中声明一个新按钮,而是重复使用它?我使用过这种类型的布局。您可以为这两个对象创建不同的Id,并放大该视图并给出不同的名称,以便区分这两个对象
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image"
/>
<include android:id="+id/firstOne" layout="@layout/buttonLayout"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
第二个是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image"
/>
<include android:id="+id/secondTwo" layout="@layout/buttonLayout"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
问题在于布局文件包含在同一布局文件中,并且该布局文件的id也包含在同一布局文件中 按钮是相同的,所以当您同时单击任何按钮时,事件将在两个按钮上触发,就像两个按钮都被单击一样。
因此,您必须为两个按钮提供不同的id我希望它工作正常 您可以为每个包含的布局添加不同的Id:
<include android:id="+id/layout_a" layout="@layout/buttonLayout"/>
但这将采取线性布局和查看按钮的权利?因此,如果我添加一个clicklistener,它将捕获事件,甚至我只单击视图而不单击按钮。我只想拿按钮。做两个不同的id。你需要膨胀不同的视图,在获得不同的视图后,你必须使用特定的视图来查找dviewByID,这样你就得到了两个不同的按钮,并为它们设置了不同的点击监听器,这样你就可以为separe按钮单独获得按钮事件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image"
/>
<include android:id="+id/secondTwo" layout="@layout/buttonLayout"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<include android:id="+id/layout_a" layout="@layout/buttonLayout"/>
<include android:id="+id/layout_b" layout="@layout/buttonLayout"/>
btnCellA = (Button)thirdView.findViewById(R.id.layout_a).findViewById(R.id.btnCell);
btnCellB = (Button)thirdView.findViewById(R.id.layout_b).findViewById(R.id.btnCell);