我正在尝试在我的应用程序中创建一个弹出窗口;但是,当按下按钮时,不会出现弹出窗口。没有错误,应用程序也不会崩溃。与之前用于弹出窗口的一些代码相比,正在使用的代码稍作修改。
ISOButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater2 = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater2.inflate(R.layout.isochanger, null);
final PopupWindow popupwindow = new PopupWindow(popupView, width, height, focusable);
popupwindow.showAtLocation(view, Gravity.CENTER, 0, 0);
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupwindow.dismiss();
return true;
}
});
}
});
完整的活动是:
package com.example.startrailscamera;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.*;
import android.os.Bundle;
import android.view.*;
import android.util.*;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
TextureView textureView;
ImageButton imageButton, singleImageButton;
TextView ISOText;
SeekBar seekBar;
Integer maxISO, minISO, progress, ISO, width, height, ISO2, ISO3, ISO4, ISORange, ISO5;
Range<Integer> rangeISO;
String cameraId;
Button ISOButton, isomin, iso2, iso3, iso4, iso5, isomax;
boolean focusable;
View popupView;
LayoutInflater inflater, inflater2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.cameralayout, null);
width = LinearLayout.LayoutParams.WRAP_CONTENT;
height = LinearLayout.LayoutParams.WRAP_CONTENT;
focusable = true;
final PopupWindow popupwindow = new PopupWindow(popupView, width, height, focusable);
popupwindow.showAtLocation(view, Gravity.CENTER, 0, 0);
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupwindow.dismiss();
return true;
}
});
}
});
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
return;
}
try {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
maxISO = rangeISO.getUpper();
minISO = rangeISO.getLower();
rangeISO = characteristics.get(CameraCharacteristics.SENSOR_INFO_SENSITIVITY_RANGE);
Range<Long> exposureTimeRange = characteristics.get(CameraCharacteristics.SENSOR_INFO_EXPOSURE_TIME_RANGE);
} catch (CameraAccessException e) {
e.printStackTrace();
}
ISORange = maxISO - minISO;
ISO2 = minISO + (ISORange/5);
ISO3 = minISO + (ISORange/5*2);
ISO4 = minISO + (ISORange/5*3);
ISO5 = minISO + (ISORange/5*4);
isomin.setText(minISO);
iso2.setText(ISO2);
iso3.setText(ISO3);
iso4.setText(ISO4);
iso5.setText(ISO5);
isomax.setText(maxISO);
ISOButton = findViewById(R.id.ISOButton);
ISOButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater2 = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater2.inflate(R.layout.isochanger, null);
final PopupWindow popupwindow = new PopupWindow(popupView, width, height, focusable);
popupwindow.showAtLocation(view, Gravity.CENTER, 0, 0);
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupwindow.dismiss();
return true;
}
});
}
});
}
}
而我要显示的XML文件是
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:id="@+id/iso3"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_toStartOf="@+id/iso4"
android:text="3" />
<Button
android:id="@+id/isomin"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="35dp"
android:layout_marginEnd="0dp"
android:layout_toStartOf="@+id/iso2"
android:text="1" />
<Button
android:id="@+id/iso4"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_toStartOf="@+id/iso5"
android:text="4" />
<Button
android:id="@+id/iso5"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_toStartOf="@+id/isomax"
android:text="5" />
<Button
android:id="@+id/isomax"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="45dp"
android:text="6" />
<Button
android:id="@+id/iso2"
android:layout_width="58dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_toStartOf="@+id/iso3"
android:text="2" />
</RelativeLayout> ```
答案 0 :(得分:1)
问题来自width
和height
。
针对此特定问题,我评论了不必要的代码。 这是您的工作活动代码
package com.andruid.magic.imagesegmentationlib;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.*;
import android.os.Bundle;
import android.view.*;
import android.util.*;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
TextureView textureView;
ImageButton imageButton, singleImageButton;
TextView ISOText;
SeekBar seekBar;
Integer maxISO, minISO, progress, ISO, width, height, ISO2, ISO3, ISO4, ISORange, ISO5;
Range<Integer> rangeISO;
String cameraId="0";
Button ISOButton, isomin, iso2, iso3, iso4, iso5, isomax;
boolean focusable = true;
View popupView;
LayoutInflater inflater, inflater2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.activity_main, null);
width = LinearLayout.LayoutParams.WRAP_CONTENT;
height = LinearLayout.LayoutParams.WRAP_CONTENT;
focusable = true;
final PopupWindow popupwindow = new PopupWindow(popupView, width, height, focusable);
popupwindow.showAtLocation(view, Gravity.CENTER, 0, 0);
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupwindow.dismiss();
return true;
}
});
}
});
/* CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
return;
}
try {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
maxISO = rangeISO.getUpper();
minISO = rangeISO.getLower();
rangeISO = characteristics.get(CameraCharacteristics.SENSOR_INFO_SENSITIVITY_RANGE);
Range<Long> exposureTimeRange = characteristics.get(CameraCharacteristics.SENSOR_INFO_EXPOSURE_TIME_RANGE);
} catch (CameraAccessException e) {
e.printStackTrace();
}
ISORange = maxISO - minISO;
ISO2 = minISO + (ISORange/5);
ISO3 = minISO + (ISORange/5*2);
ISO4 = minISO + (ISORange/5*3);
ISO5 = minISO + (ISORange/5*4);
isomin.setText(minISO);
iso2.setText(ISO2);
iso3.setText(ISO3);
iso4.setText(ISO4);
iso5.setText(ISO5);
isomax.setText(maxISO);
*/
ISOButton = findViewById(R.id.ISOButton);
ISOButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
inflater2 = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater2.inflate(R.layout.isochanger, null);
width = LinearLayout.LayoutParams.WRAP_CONTENT;
height = LinearLayout.LayoutParams.WRAP_CONTENT;
Log.d("TAG",""+width);
final PopupWindow popupwindow = new PopupWindow(popupView, width, height, focusable);
popupwindow.showAtLocation(view, Gravity.CENTER, 0, 0);
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupwindow.dismiss();
return true;
}
});
}
});
}
}
这是输出屏幕截图。
希望它可以帮助您解决问题。
答案 1 :(得分:0)
尝试
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
答案 2 :(得分:0)
您也可以使用Dialog来实现弹出窗口。试试这个。
activity_main.xml:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_22"
android:text="Select"
android:textColor="@color/black"
android:textSize="@dimen/sp_17"
tools:ignore="HardcodedText" />
</RelativeLayout>
design_popup.xml:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_15"
android:gravity="center"
android:text="Radio Buttons"
android:textColor="@color/black"
android:textSize="@dimen/sp_17"
tools:ignore="HardcodedText" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_margin="@dimen/dp_20"
android:orientation="vertical">
<RadioButton
android:id="@+id/radio_xyz"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_10"
android:text="xyz"
android:textSize="@dimen/sp_17"
tools:ignore="HardcodedText" />
<RadioButton
android:id="@+id/radio_abc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_10"
android:text="abc"
android:textSize="@dimen/sp_17"
tools:ignore="HardcodedText" />
<RadioButton
android:id="@+id/radio_def"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_10"
android:text="def"
android:textSize="@dimen/sp_17"
tools:ignore="HardcodedText" />
<RadioButton
android:id="@+id/radio_ghi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_10"
android:text="ghi"
android:textSize="@dimen/sp_17"
tools:ignore="HardcodedText" />
</RadioGroup>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/radio_group">
<TextView
android:id="@+id/tv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/dp_70"
android:text="Cancel"
android:layout_alignParentEnd="true"
android:textColor="@color/black"
tools:ignore="HardcodedText"
android:layout_alignParentRight="true"
android:layout_toLeftOf="@+id/tv_choose"
android:layout_toStartOf="@+id/tv_choose"
android:layout_marginEnd="@dimen/dp_70" />
<TextView
android:id="@+id/tv_choose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose"
android:textColor="@color/black"
tools:ignore="HardcodedText"
android:layout_marginRight="@dimen/dp_10"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="@dimen/dp_10" />
</RelativeLayout>
MainActivity.java
TextView tv_select;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
tv_select = findViewById(R.id.tv_select);
tv_select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final TextView tv_choose;
final TextView tv_cancel;
final Dialog builder = new Dialog(context);
LayoutInflater layoutInflater = LayoutInflater.from(context);
final View dialog = layoutInflater.inflate(R.layout.design_popup, null);
RadioGroup radio_group = dialog.findViewById(R.id.radio_group);
final RadioButton radio_xyz = dialog.findViewById(R.id.radio_xyz);
final RadioButton radio_abc = dialog.findViewById(R.id.radio_abc);
final RadioButton radio_def = dialog.findViewById(R.id.radio_def);
final RadioButton radio_ghi = dialog.findViewById(R.id.radio_ghi);
tv_choose = dialog.findViewById(R.id.tv_choose);
tv_cancel = dialog.findViewById(R.id.tv_cancel);
switch (tv_select.getText().toString()) {
case "xyz":
radio_group.check(R.id.radio_xyz);
break;
case "abc":
radio_group.check(R.id.radio_abc);
break;
case "def":
radio_group.check(R.id.radio_def);
break;
case "ghi":
radio_group.check(R.id.radio_ghi);
break;
}
radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio_xyz:
tv_choose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String value = radio_xyz.getText().toString();
tv_select.setText(value);
builder.dismiss();
}
});
break;
case R.id.radio_abc:
tv_choose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String value = radio_abc.getText().toString();
tv_select.setText(value);
builder.dismiss();
}
});
break;
case R.id.radio_def:
tv_choose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String value = radio_def.getText().toString();
tv_select.setText(value);
builder.dismiss();
}
});
break;
case R.id.radio_ghi:
tv_choose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String value = radio_ghi.getText().toString();
tv_select.setText(value);
builder.dismiss();
}
});
break;
}
}
});
builder.setContentView(dialog);
builder.show();
tv_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("macro", "asd");
builder.cancel();
builder.dismiss();
}
});
}
});
}