java Android Studio:在不同的活动中使用“OnClick”方法?
我创建了安卓:onClick方法“showHiddenText”,在两个对话框中使用开关盒来扩展不同的按钮
按钮示例:
<Button
安卓:id="@+id/product_description_Btn"
安卓:layout_height="60sp"
安卓:layout_width="350sp"
安卓:layout_gravity="center"
安卓:background="@drawable/button_lightblue_normal"
安卓:drawableBottom="@drawable/ic_baseline_keyboard_arrow_down_24"
安卓:textSize="18sp"
安卓:paddingTop="7sp"
安卓:text="@string/product_information_descriptionBtn"
安卓:textAllCaps="false"
安卓:onClick="showHiddenText"
安卓:fontFamily="@font/louis_bold"/>
<LinearLayout
安卓:id="@+id/expandable_view_description"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:background="@color/lighter_grey"
安卓:orientation="vertical"
安卓:visibility="gone"
安卓:layout_margin="10sp">
<TextView
安卓:id="@+id/expandable_text_description"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Description"
安卓:textColor="@color/black"
安卓:fontFamily="@font/louis_bold"
安卓:textSize="18sp"
安卓:layout_marginLeft="30sp"
安卓:layout_marginTop="20sp"
安卓:layout_marginRight="10sp"
安卓:layout_marginBottom="20sp"/>
</LinearLayout>
<Button
安卓:id="@+id/product_consumptionadvice_Btn"
安卓:layout_height="60sp"
安卓:layout_width="350sp"
安卓:layout_gravity="center"
安卓:layout_marginTop="10sp"
安卓:background="@drawable/button_lightblue_normal"
安卓:drawableBottom="@drawable/ic_baseline_keyboard_arrow_down_24"
安卓:textSize="18sp"
安卓:paddingTop="7sp"
安卓:text="@string/product_information_consumptionadviceBtn"
安卓:textAllCaps="false"
安卓:onClick="showHiddenText"
安卓:fontFamily="@font/louis_bold"/>
<LinearLayout
安卓:id="@+id/expandable_view_product_consumptionadvice"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:background="@color/lighter_grey"
安卓:orientation="vertical"
安卓:visibility="gone"
安卓:layout_margin="10sp">
<TextView
安卓:id="@+id/expandable_text_product_consumptionadvice"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="ConsumptionAdvice"
安卓:textColor="@color/black"
安卓:fontFamily="@font/louis_bold"
安卓:textSize="18sp"
安卓:layout_marginLeft="30sp"
安卓:layout_marginTop="20sp"
安卓:layout_marginRight="10sp"
安卓:layout_marginBottom="20sp"/>
</LinearLayout>
打开这些对话框的按钮称为“产品信息”和“制造商信息”
我在活动a中创建了这个方法,在那里可以访问按钮
但现在我创建了另一个活动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(视图)在视图类上定义的安卓:onClick属性
所以我的问题是:
有没有办法在Activtiy a中访问这个onClick方法?还是我必须在活动B中再次实现该方法
谢谢你的帮助
# 1 楼答案
有很多方法可以实现它