gpt4 book ai didi

android - 如何在 android 布局的多个组件中拥有一个唯一按钮?

转载 作者:搜寻专家 更新时间:2023-11-01 08:33:45 24 4
gpt4 key购买 nike

所以我有一个 xml,它由一个包含 Button 和 TextView 的线性布局组成,如下所示:

 <?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>

我想在不同的 xml 中的其他布局中使用相同的布局。我每次都需要同一个按钮,所以我通过将它包含在两个布局中来重用它(两个布局都在同一个 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="match_parent"
android:orientation="vertical">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image"
/>

<include 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 layout="@layout/buttonLayout"/>

<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

所以我在应用程序的开头显示第一个布局并隐藏第二个布局,当用户在界面内移动时,布局会交换,以便显示另一个布局并隐藏第一个布局。

问题是我在我的 java Activity 类中声明了 Button,如下所示:

   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");
}

问题是,当我在第一个 View 显示而第二个 View 隐藏时单击按钮时,按钮工作正常,但是当我取消隐藏第二个布局,隐藏第一个布局,然后继续单击按钮时,应该与第一个相同,但布局不同,没有任何反应。我搜索并发现,发生这种情况是因为由于 View 层次结构,id 仅分配给第一个布局中显示的按钮,而不是第二个布局中的按钮。如何让两个按钮对同一操作使用react,而不是在每个布局中声明一个新按钮而是重复使用它?

最佳答案

我用过这种布局。您可以为两者创建不同的 Id 并扩展该 View 并提供不同的名称,以便您可以区分两者。

<?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>

android 第二个是

 <?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>

关于android - 如何在 android 布局的多个组件中拥有一个唯一按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38388577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com