java - 创建弹出窗口的问题
问题描述
我正在尝试在我的应用程序中创建一个弹出窗口;但是,当按下按钮时,不会出现弹出窗口。没有错误,应用程序不会崩溃。正在使用的代码是从我之前用于弹出窗口的一些代码稍微修改的,它可以工作。
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> ```
解决方案
问题是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;
}
});
}
});
}
}
这是输出屏幕截图。
希望它可以帮助您修复。
推荐阅读
- python-3.x - 如何将 1 个系列的唯一值作为列并计算每季度系列中唯一值的每次出现?
- c# - C#/Linq - 按字符串数组中返回的第二项分组,同时保留父项
- c# - 字段为空的实体代码第一个问题
- reactjs - 运行 truffle unbox 后反应出现错误
- python - 我制作了列表的副本,但是当我对副本进行更改时,原始列表仍然会更改。我不明白为什么
- jsf - 如何正确使用 selectOneMenu 与 selectItems 和 Converter
- npm - 从 NPM v0.2.0 安装 Hyperledger Caliper CLI 的 pkcs11js@1.0.19 脚本失败
- php - 如何计算数组的值
- javascript - 如何通过 document.referrer 控制 css
- android - Android初始化服务错误无效关键字'u:object_r:system_file:s0