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

拉韦林克斯

所以我有一个由线性布局组成的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活动类中声明了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");
    }

问题是,当我在显示第一个视图而第二个视图隐藏时单击按钮时,该按钮可以正常工作,但是当我取消隐藏第二个布局时,隐藏第一个布局并继续单击该按钮,那应该是与第一个相同,但布局不同,没有任何反应。我进行搜索后发现,发生这种情况的原因是,由于视图层次结构,仅将ID分配给了第一种布局中显示的按钮,而没有将其分配给第二种布局中的按钮。如何使两个按钮对同一动作做出反应,而不必在每个布局中声明一个新按钮,而是重新使用它?

Jitendra ramoliya

我已经使用了这种类型的布局。您可以为两者创建不同的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>

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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

嗨,我如何在一个活动中拥有多个图形?(MPAndroid图表)

来自分类Dev

如何在一个View Controller中拥有多个Collection View?

来自分类Dev

Soundcloud:如何在一个播放器中拥有多个曲目

来自分类Dev

如何在一个View Controller中拥有多个Collection View?

来自分类Dev

如何在多个文件中拥有一个工厂

来自分类Dev

如何在另一个集合中拥有一个集合?

来自分类Dev

如何从拥有多个页面的UIPageViewController中删除一个页面

来自分类Dev

在JIRA中,如何拥有一个包含多个项目的敏捷开发板?

来自分类Dev

如何从拥有多个页面的UIPageViewController中删除一个页面

来自分类Dev

在JIRA中,如何拥有一个包含多个项目的敏捷开发板?

来自分类Dev

在 CakePHP 中,如何拥有一个表属于多个其他表 hasMany AnotherTable

来自分类Dev

如何在实体框架中拥有一个只有外键的表

来自分类Dev

如何为一个模板拥有一个控制器的多个实例

来自分类Dev

在模板中,我如何拥有一个柜台?

来自分类Dev

如何在git中拥有一个同步默认但被提交忽略的文件?

来自分类Dev

我可以在 Android 上拥有一个没有布局的应用页面吗?

来自分类Dev

如何在Android中的另一个线性布局下面设置一个线性布局

来自分类Dev

如何在Android中的另一个线性布局下面设置一个线性布局

来自分类Dev

如何在一个函数或类中为拖放中的多个组件处理多个放置?

来自分类Dev

如何在一个 redux-form Field 组件中关联多个组件?

来自分类Dev

如何在一个活动中制作一个按钮来修改其他活动中的组件

来自分类Dev

如何在Flutter中的多个按钮之间更改一个按钮的颜色?

来自分类Dev

Noflo:如何拥有只有一个组件的网络

来自分类Dev

如何在GridBagLayout组件中设置最后一个按钮的行高?

来自分类Dev

如何在Android中的另一个按钮中基于onclick启用按钮?

来自分类Dev

如何在Android中的另一个布局xml文件中创建Button对象

来自分类Dev

如何在两个文件中拥有一个类和超类(Typescript)

来自分类Dev

如何在两个文件中拥有一个类和超类(Typescript)

Related 相关文章

  1. 1

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

  2. 2

    嗨,我如何在一个活动中拥有多个图形?(MPAndroid图表)

  3. 3

    如何在一个View Controller中拥有多个Collection View?

  4. 4

    Soundcloud:如何在一个播放器中拥有多个曲目

  5. 5

    如何在一个View Controller中拥有多个Collection View?

  6. 6

    如何在多个文件中拥有一个工厂

  7. 7

    如何在另一个集合中拥有一个集合?

  8. 8

    如何从拥有多个页面的UIPageViewController中删除一个页面

  9. 9

    在JIRA中,如何拥有一个包含多个项目的敏捷开发板?

  10. 10

    如何从拥有多个页面的UIPageViewController中删除一个页面

  11. 11

    在JIRA中,如何拥有一个包含多个项目的敏捷开发板?

  12. 12

    在 CakePHP 中,如何拥有一个表属于多个其他表 hasMany AnotherTable

  13. 13

    如何在实体框架中拥有一个只有外键的表

  14. 14

    如何为一个模板拥有一个控制器的多个实例

  15. 15

    在模板中,我如何拥有一个柜台?

  16. 16

    如何在git中拥有一个同步默认但被提交忽略的文件?

  17. 17

    我可以在 Android 上拥有一个没有布局的应用页面吗?

  18. 18

    如何在Android中的另一个线性布局下面设置一个线性布局

  19. 19

    如何在Android中的另一个线性布局下面设置一个线性布局

  20. 20

    如何在一个函数或类中为拖放中的多个组件处理多个放置?

  21. 21

    如何在一个 redux-form Field 组件中关联多个组件?

  22. 22

    如何在一个活动中制作一个按钮来修改其他活动中的组件

  23. 23

    如何在Flutter中的多个按钮之间更改一个按钮的颜色?

  24. 24

    Noflo:如何拥有只有一个组件的网络

  25. 25

    如何在GridBagLayout组件中设置最后一个按钮的行高?

  26. 26

    如何在Android中的另一个按钮中基于onclick启用按钮?

  27. 27

    如何在Android中的另一个布局xml文件中创建Button对象

  28. 28

    如何在两个文件中拥有一个类和超类(Typescript)

  29. 29

    如何在两个文件中拥有一个类和超类(Typescript)

热门标签

归档