[英]Button OnClick from Fragment to MainActivity
我的MainActivity
中有 3 個片段,在我的一個片段中有 3 個ImageView
並且每個圖像視圖都根據選擇的ImageView
執行相同的操作,現在我想要實現的是,而不是分別為每個視圖使用OnClickListener
,使用 switch-case 或 if-else 調用相同的方法。 但是問題是fragment里面的那些ImageViews
不能調用MainActivity里面實現的函數。 例如。 這是理解我想說的話的代碼:
主要活動
public void imageClicked(View v){
if(v.getID() == findViewById(R.id.id_of_imageView_1).getID()){
myTextView.setText("ImageView 1 is Clicked")
}
else if(v.getID() == findViewById(R.id.id_of_imageView_2).getID()){
myTextView.setText("ImageView 2 is Clicked")
}
else if(v.getID() == findViewById(R.id.id_of_imageView_3).getID()){
myTextView.setText("ImageView 3 is Clicked")
}
}
XML (fragment_images.xml) 圖像存在的地方
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/id_of_imageView_1"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="@drawable/laptop" />
<ImageView
android:id="@+id/id_of_imageView_2"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="@drawable/memory" />
<ImageView
android:id="@+id/id_of_imageView_3"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
android:src="@drawable/screen" />
</LinearLayout>
</ScrollView>
XML (activity_main.xml) 這里我鏈接我的片段和 TextView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
//CHANGE THIS TEXT VIEW WHEN IMAGE IS CLICKED
<TextView
android:id="@+id/myTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="Select Product"
android:textSize="20sp"
android:textStyle="bold" />
<fragment
android:id="@+id/fragment_1"
android:name="com.salmanibrahim.calssifiedelectronics.ListFrag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_list" />
<fragment
android:id="@+id/fragment_2"
android:name="com.salmanibrahim.calssifiedelectronics.DetailFrag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_detail" />
//THIS IS THE FRAGMENT
<fragment
android:id="@+id/fragment_images"
android:name="com.salmanibrahim.calssifiedelectronics.AddProduct"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/fragment_images" />
</LinearLayout>
如何在 ImageView 上使用onClick
調用MainActivity
中定義的ImageView
imageClicked()
在您的片段中,您可以使用requireActivity()
或getActivity()
) 在MainActivity
中調用imageClicked()
) 並執行必要的轉換
在片段中:
public View onCreateView(...) {
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
MainActivity mActivity = (MainActivity) requireActivity();
imageView1 = view.findViewById(...);
imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mActivity.imageClicked(imageView1);
}
});
...}
對 imageView2、3 重復相同的操作
此外, imageClicked()
中的條件似乎不正確,因為您findViewById
將指向您的 MainActivity 布局,而不是片段。
所以你需要改變這個
if (v.getID() == findViewById(R.id.id_of_imageView_1).getID())
至
if (v.getID() == R.id.id_of_imageView_1)
並為其他圖像做同樣的事情。
我了解您要做什么,但android:onClick
參數僅鏈接tools:context
字段。
因此,無法從片段的ImageView
中引用您在 MainActivity 中找到的imageClicked()
function,因為它們處於不同的上下文中。 檢查此以獲取更多信息。
你做錯了。 您可以改為這樣做。
imageView_1.setOnClickListener(this);
然后從View.onClickListener
實現活動
然后通過 id 查看已實現的方法。
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.id_of_imageView_1:
// do your things here
break;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.