【发布时间】: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 中再次实现该方法?
感谢您的帮助!
【问题讨论】:
-
请添加其他人可以看到和复制的实际代码