gpt4 book ai didi

java - android studio - 如何将微调器添加到服务中的弹出窗口

转载 作者:行者123 更新时间:2023-12-01 11:52:53 30 4
gpt4 key购买 nike

我这里遇到了一些问题!我的代码显示了我的服务中的弹出窗口,并且在该弹出窗口内有一个微调器,当我单击微调器时,错误开始向我展示:'(这是使用的代码:

            final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_window, null, false), ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
final Spinner spn_ = (Spinner)pw.getContentView().findViewById(R.id.spinner3);
final String[] options_alma_ = {"Hello","HI"};
final ArrayAdapter<String> stringArrayAdapter_ = new ArrayAdapter<String>(AlMA_Service.this, android.R.layout.simple_spinner_dropdown_item, options_alma_);
final Button btn = (Button)pw.getContentView().findViewById(R.id.button10);
btn.setText("Choose Hello");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Clicked", "Here Before Dismiss !!");
pw.dismiss();
}
});
spn_.setAdapter(stringArrayAdapter_);
spn_.setSelection(stringArrayAdapter_.getPosition(Locale.getDefault().getDisplayName()));
spn_.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
try {
btn.setText("Choose " + stringArrayAdapter_.getItem(position));
} catch (NullPointerException e) {
e.printStackTrace();
btn.setText("Choose Hello" );
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
pw.showAsDropDown(mView);

有什么帮助吗?

最佳答案

您的代码的问题是您无法可靠地将当前 Activity 的 Activity 传递到弹出窗口 会有一些不可靠地传递 Activity 的方式,但我不推荐它

你可以尝试这个而不是弹出菜单

windowManager2 = (WindowManager)getSystemService(WINDOW_SERVICE);
LayoutInflater layoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.popupmenu, null);
params=new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
params.gravity=Gravity.CENTER|Gravity.CENTER;
params.x=0;
params.y=0;
windowManager2.addView(view, params);

关于java - android studio - 如何将微调器添加到服务中的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28657007/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com