在 android 中单击不同的图像按钮时弹出的 if else 语句
If else statement of popup when click different image button in android
我遇到了一些关于弹出窗口的 if else 语句的问题 window.i 想设置当我点击不同的图像按钮时弹出不同的图像,但是现在在我编码并测试弹出窗口之后没有功能...
这是我的java代码
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.macarons, container, false);
ImageButton ma1 = (ImageButton) view.findViewById(R.id.ma1);
ImageButton ma2 = (ImageButton) view.findViewById(R.id.ma2);
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
final int height = size.y;
positionPopup = (RelativeLayout) view.findViewById(R.id.popup_position);
if (view.equals(ma1)) {
ma1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
View customView = inflater.inflate(R.layout.popup, null);
ImageView img1 = (ImageView) customView.findViewById(R.id.popup_img);
img1.setImageResource(R.drawable.ma_bananacho_popup);
myPopUp = new PopupWindow(customView);
myPopUp.setWidth(width - 50);
myPopUp.setHeight(height - 50);
myPopUp.setOutsideTouchable(true);
myPopUp.setFocusable(true);
myPopUp.showAtLocation(positionPopup, Gravity.CENTER, 0, 0);
}
});
}
else if (view.equals(ma2)) {
ma2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
View customView = inflater.inflate(R.layout.popup, null);
ImageView img2 = (ImageView) customView.findViewById(R.id.popup_img);
img2.setImageResource(R.drawable.chocolate);
myPopUp = new PopupWindow(customView);
myPopUp.setWidth(width - 50);
myPopUp.setHeight(height - 50);
myPopUp.setOutsideTouchable(true);
myPopUp.setFocusable(true);
myPopUp.showAtLocation(positionPopup, Gravity.CENTER, 0, 0);
}
});
}
return view;
}
这是我的弹窗xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/popup_img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</RelativeLayout>
这是片段xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:layout_alignParentTop="true"
tools:ignore="ContentDescription">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF"
android:id="@+id/popup_position">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp">
<ImageButton
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:background="@drawable/ma_bananacho"
android:gravity="center"
android:layout_gravity="fill_horizontal"
android:id="@+id/ma1"/>
<ImageButton
android:id="@+id/ma2"
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:layout_gravity="fill_horizontal"
android:background="@drawable/ma_blueberry"
android:gravity="center" />
<ImageButton
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:background="@drawable/ma_chomint"
android:gravity="center"
android:layout_gravity="fill_horizontal" />
<ImageButton
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:background="@drawable/ma_coffee"
android:gravity="center"
android:layout_gravity="fill_horizontal" />
<ImageButton
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:background="@drawable/ma_greentea"
android:gravity="center"
android:layout_gravity="fill_horizontal" />
<ImageButton
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:background="@drawable/ma_strawcho"
android:gravity="center"
android:layout_gravity="fill_horizontal" />
</GridLayout>
</RelativeLayout>
</ScrollView>
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
View customView = inflater.inflate(R.layout.popup, null);
ImageView img1 = (ImageView) customView.findViewById(R.id.popup_img);
if (v.getId() == R.id.ma1) {
img1.setImageResource(R.drawable.ma_bananacho_popup);
} else if (v.getId() == R.id.ma2) {
img1.setImageResource(R.drawable.chocolate);
}
myPopUp = new PopupWindow(customView);
myPopUp.setWidth(width - 50);
myPopUp.setHeight(height - 50);
myPopUp.setOutsideTouchable(true);
myPopUp.setFocusable(true);
myPopUp.showAtLocation(positionPopup, Gravity.CENTER, 0, 0);
}
};
ma1.setOnClickListener(clickListener);
ma2.setOnClickListener(clickListener);
- 创建一个通用的 clickListener 并将其设置为 2 个图像视图
ma1, ma2
- 通过比较
viewIds
更改弹窗的图片资源如下
in java 任何覆盖方法在 if 条件内部都不起作用,因为覆盖方法应该直接进入 class 内部。因此,当您使用任何覆盖方法时,您必须将其放入 class 或直接放入 class 。你使用匿名吗
class 所以你必须把 if else 放在覆盖 onclick 方法中。例如
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(condition){
//for if condition is true
}else{
//for if condition is false
}
}
};
我遇到了一些关于弹出窗口的 if else 语句的问题 window.i 想设置当我点击不同的图像按钮时弹出不同的图像,但是现在在我编码并测试弹出窗口之后没有功能...
这是我的java代码
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.macarons, container, false);
ImageButton ma1 = (ImageButton) view.findViewById(R.id.ma1);
ImageButton ma2 = (ImageButton) view.findViewById(R.id.ma2);
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
final int height = size.y;
positionPopup = (RelativeLayout) view.findViewById(R.id.popup_position);
if (view.equals(ma1)) {
ma1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
View customView = inflater.inflate(R.layout.popup, null);
ImageView img1 = (ImageView) customView.findViewById(R.id.popup_img);
img1.setImageResource(R.drawable.ma_bananacho_popup);
myPopUp = new PopupWindow(customView);
myPopUp.setWidth(width - 50);
myPopUp.setHeight(height - 50);
myPopUp.setOutsideTouchable(true);
myPopUp.setFocusable(true);
myPopUp.showAtLocation(positionPopup, Gravity.CENTER, 0, 0);
}
});
}
else if (view.equals(ma2)) {
ma2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
View customView = inflater.inflate(R.layout.popup, null);
ImageView img2 = (ImageView) customView.findViewById(R.id.popup_img);
img2.setImageResource(R.drawable.chocolate);
myPopUp = new PopupWindow(customView);
myPopUp.setWidth(width - 50);
myPopUp.setHeight(height - 50);
myPopUp.setOutsideTouchable(true);
myPopUp.setFocusable(true);
myPopUp.showAtLocation(positionPopup, Gravity.CENTER, 0, 0);
}
});
}
return view;
}
这是我的弹窗xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/popup_img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</RelativeLayout>
这是片段xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:layout_alignParentTop="true"
tools:ignore="ContentDescription">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF"
android:id="@+id/popup_position">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:layout_marginTop="50dp"
android:layout_marginBottom="50dp">
<ImageButton
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:background="@drawable/ma_bananacho"
android:gravity="center"
android:layout_gravity="fill_horizontal"
android:id="@+id/ma1"/>
<ImageButton
android:id="@+id/ma2"
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:layout_gravity="fill_horizontal"
android:background="@drawable/ma_blueberry"
android:gravity="center" />
<ImageButton
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:background="@drawable/ma_chomint"
android:gravity="center"
android:layout_gravity="fill_horizontal" />
<ImageButton
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:background="@drawable/ma_coffee"
android:gravity="center"
android:layout_gravity="fill_horizontal" />
<ImageButton
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:background="@drawable/ma_greentea"
android:gravity="center"
android:layout_gravity="fill_horizontal" />
<ImageButton
android:layout_width="0dp"
android:layout_height="190dp"
android:layout_columnWeight="1"
android:background="@drawable/ma_strawcho"
android:gravity="center"
android:layout_gravity="fill_horizontal" />
</GridLayout>
</RelativeLayout>
</ScrollView>
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
View customView = inflater.inflate(R.layout.popup, null);
ImageView img1 = (ImageView) customView.findViewById(R.id.popup_img);
if (v.getId() == R.id.ma1) {
img1.setImageResource(R.drawable.ma_bananacho_popup);
} else if (v.getId() == R.id.ma2) {
img1.setImageResource(R.drawable.chocolate);
}
myPopUp = new PopupWindow(customView);
myPopUp.setWidth(width - 50);
myPopUp.setHeight(height - 50);
myPopUp.setOutsideTouchable(true);
myPopUp.setFocusable(true);
myPopUp.showAtLocation(positionPopup, Gravity.CENTER, 0, 0);
}
};
ma1.setOnClickListener(clickListener);
ma2.setOnClickListener(clickListener);
- 创建一个通用的 clickListener 并将其设置为 2 个图像视图
ma1, ma2
- 通过比较
viewIds
更改弹窗的图片资源如下
in java 任何覆盖方法在 if 条件内部都不起作用,因为覆盖方法应该直接进入 class 内部。因此,当您使用任何覆盖方法时,您必须将其放入 class 或直接放入 class 。你使用匿名吗 class 所以你必须把 if else 放在覆盖 onclick 方法中。例如
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(condition){
//for if condition is true
}else{
//for if condition is false
}
}
};