【问题标题】:Android Studio: Use "OnClick" Method in different Activity?Android Studio:在不同的活动中使用“OnClick”方法?
【发布时间】:2021-01-17 12:40:42
【问题描述】:

我创建了 android:onClick 方法 "showHiddenText" 来扩展不同的按钮,并在两个对话框中使用开关盒。

按钮示例:

<Button
                android:id="@+id/product_description_Btn"
                android:layout_height="60sp"
                android:layout_width="350sp"
                android:layout_gravity="center"
                android:background="@drawable/button_lightblue_normal"
                android:drawableBottom="@drawable/ic_baseline_keyboard_arrow_down_24"
                android:textSize="18sp"
                android:paddingTop="7sp"
                android:text="@string/product_information_descriptionBtn"
                android:textAllCaps="false"
                android:onClick="showHiddenText"
                android:fontFamily="@font/louis_bold"/>

            <LinearLayout
                android:id="@+id/expandable_view_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/lighter_grey"
                android:orientation="vertical"
                android:visibility="gone"
                android:layout_margin="10sp">


                <TextView
                    android:id="@+id/expandable_text_description"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Description"
                    android:textColor="@color/black"
                    android:fontFamily="@font/louis_bold"
                    android:textSize="18sp"
                    android:layout_marginLeft="30sp"
                    android:layout_marginTop="20sp"
                    android:layout_marginRight="10sp"
                    android:layout_marginBottom="20sp"/>


            </LinearLayout>


            <Button
                android:id="@+id/product_consumptionadvice_Btn"
                android:layout_height="60sp"
                android:layout_width="350sp"
                android:layout_gravity="center"
                android:layout_marginTop="10sp"
                android:background="@drawable/button_lightblue_normal"
                android:drawableBottom="@drawable/ic_baseline_keyboard_arrow_down_24"
                android:textSize="18sp"
                android:paddingTop="7sp"
                android:text="@string/product_information_consumptionadviceBtn"
                android:textAllCaps="false"
                android:onClick="showHiddenText"
                android:fontFamily="@font/louis_bold"/>

            <LinearLayout
                android:id="@+id/expandable_view_product_consumptionadvice"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/lighter_grey"
                android:orientation="vertical"
                android:visibility="gone"
                android:layout_margin="10sp">


                <TextView
                    android:id="@+id/expandable_text_product_consumptionadvice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="ConsumptionAdvice"
                    android:textColor="@color/black"
                    android:fontFamily="@font/louis_bold"
                    android:textSize="18sp"
                    android:layout_marginLeft="30sp"
                    android:layout_marginTop="20sp"
                    android:layout_marginRight="10sp"
                    android:layout_marginBottom="20sp"/>


            </LinearLayout>

打开这些对话框的按钮称为“产品信息”和“制造商信息”

我在 Activity A 中创建了这个方法,其中的按钮可以访问。

但现在我创建了另一个 Activity B,其中的按钮也可以访问以执行完全相同的操作。

showHiddenText-代码:

public void showHiddenText(View view) {

    switch (view.getId()) {
        //----------------------------------PRODUKT-INFORMATIONEN-----------------------------------------------------//
        case R.id.product_description_Btn:
            expandable_CardView = (CardView) product_info_view.findViewById(R.id.product_info_cardview);
            expandable_View = (LinearLayout) product_info_view.findViewById(R.id.expandable_view_description);
            expand_Btn = (Button) product_info_view.findViewById(R.id.product_description_Btn);
            break;

        case R.id.product_consumptionadvice_Btn:
            expandable_CardView = (CardView) product_info_view.findViewById(R.id.product_info_cardview);
            expandable_View = (LinearLayout) product_info_view.findViewById(R.id.expandable_view_product_consumptionadvice);
            expand_Btn = (Button) product_info_view.findViewById(R.id.product_consumptionadvice_Btn);
            break;

        case R.id.product_ingredients_Btn:
            expandable_CardView = (CardView) product_info_view.findViewById(R.id.product_info_cardview);
            expandable_View = (LinearLayout) product_info_view.findViewById(R.id.expandable_view_product_ingredients);
            expand_Btn = (Button) product_info_view.findViewById(R.id.product_ingredients_Btn);
            break;

        case R.id.product_nutritionvalues_Btn:
            expandable_CardView = (CardView) product_info_view.findViewById(R.id.product_info_cardview);
            expandable_View = (LinearLayout) product_info_view.findViewById(R.id.expandable_view_product_nutritionvalues);
            expand_Btn = (Button) product_info_view.findViewById(R.id.product_nutritionvalues_Btn);
            break;

        case R.id.product_bestbefore_Btn:
            expandable_CardView = (CardView) product_info_view.findViewById(R.id.product_info_cardview);
            expandable_View = (LinearLayout) product_info_view.findViewById(R.id.expandable_view_product_bestbefore);
            expand_Btn = (Button) product_info_view.findViewById(R.id.product_bestbefore_Btn);
            break;

        case R.id.product_allergies_Btn:
            expandable_CardView = (CardView) product_info_view.findViewById(R.id.product_info_cardview);
            expandable_View = (LinearLayout) product_info_view.findViewById(R.id.expandable_view_product_allergies);
            expand_Btn = (Button) product_info_view.findViewById(R.id.product_allergies_Btn);
            break;

        //----------------------------------HERSTELLER-INFORMATIONEN-----------------------------------------------------//
        case R.id.manufactorer_info_name_Btn:
            expandable_CardView = (CardView) manufactorer_info_view.findViewById(R.id.manufactorer_info_cardview);
            expandable_View = (LinearLayout) manufactorer_info_view.findViewById(R.id.expandable_view_manufactorer_name);
            expand_Btn = (Button) manufactorer_info_view.findViewById(R.id.manufactorer_info_name_Btn);
            break;

        case R.id.manufactorer_info_production_Btn:
            expandable_CardView = (CardView) manufactorer_info_view.findViewById(R.id.manufactorer_info_cardview);
            expandable_View = (LinearLayout) manufactorer_info_view.findViewById(R.id.expandable_view_manufactorer_production);
            expand_Btn = (Button) manufactorer_info_view.findViewById(R.id.manufactorer_info_production_Btn);
            break;


        case R.id.manufactorer_info_animals_Btn:
            expandable_CardView = (CardView) manufactorer_info_view.findViewById(R.id.manufactorer_info_cardview);
            expandable_View = (LinearLayout) manufactorer_info_view.findViewById(R.id.expandable_view_manufactorer_animals);
            expand_Btn = (Button) manufactorer_info_view.findViewById(R.id.manufactorer_info_animals_Btn);
            break;

        case R.id.manufactorer_info_contact_Btn:
            expandable_CardView = (CardView) manufactorer_info_view.findViewById(R.id.manufactorer_info_cardview);
            expandable_View = (LinearLayout) manufactorer_info_view.findViewById(R.id.expandable_view_manufactorer_contact);
            expand_Btn = (Button) manufactorer_info_view.findViewById(R.id.manufactorer_info_contact_Btn);
            break;

        default:
            break;
    }

    ExpandView(expand_Btn, expandable_View);

}

public void ExpandView(Button ExpandButton, LinearLayout ExpandView){

    if (ExpandView.getVisibility() == View.GONE) {
        TransitionManager.beginDelayedTransition(expandable_CardView, new AutoTransition());
        ExpandView.setVisibility(View.VISIBLE);
        ExpandButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.ic_baseline_keyboard_arrow_up_24);
    }
    else {
        TransitionManager.beginDelayedTransition(expandable_CardView, new AutoTransition());
        ExpandView.setVisibility(View.GONE);
        ExpandButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.ic_baseline_keyboard_arrow_down_24);
    }
}

没有再次创建方法“showHiddenText”,我得到了错误

在视图类上定义的父或祖先上下文中找不到方法 showHiddenText(View) android:onClick 属性.....

所以我的问题是:

有没有办法在 Activtiy A 中访问这个 onClick 方法?还是我必须在 Activity B 中再次实现该方法?

感谢您的帮助!

【问题讨论】:

  • 请添加其他人可以看到和复制的实际代码

标签: java android methods


【解决方案1】:
  1. 如果您使用 Kotlin,您可以为 Activity 创建扩展。
  2. 您可以使用此方法创建 BaseActivity。
  3. 您可以使用此方法创建 Helper 类。

有很多方法可以实现它。

【讨论】:

  • 嗯.. BaseActivity 你的意思是像一个 Activtiy,我在几个不同的 Activity 中需要的所有方法和变量都被创建?
  • 是这样的。恕我直言,不推荐使用该选项,因为 BaseActivity 可能成为“GodActivity”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
相关资源
最近更新 更多