我面临一些关于弹出窗口的if else语句的问题。我想设置当我点击不同的图像按钮然后会弹出不同的图像,但现在我编码和测试弹出后没有功能。 ..
这是我的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>
答案 0 :(得分:0)
Session
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);
ma1, ma2
如下答案 1 :(得分:0)
在java中任何overide方法都不会在内部工作如果条件因为overide方法应该直接在class里面。因此,当你使用任何一种覆盖方法时,你必须在课堂下或在课堂上直接进行。你是匿名的吗? 因此你必须将if else置于onclick方法之外。对于exa。
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
}
}
};