android studio - 如何将微调器添加到服务中的弹出窗口
android studio - how to add spinner to popupwindow in service
我这里有一些问题!!我有代码显示来自我的服务的弹出窗口,在这个弹出窗口内有一个微调器,当我单击微调器时,错误开始向我展示 :'(
这是使用的代码:
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 传递给弹出窗口 window
会有一些不可靠的传递方式 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);
我这里有一些问题!!我有代码显示来自我的服务的弹出窗口,在这个弹出窗口内有一个微调器,当我单击微调器时,错误开始向我展示 :'( 这是使用的代码:
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 传递给弹出窗口 window 会有一些不可靠的传递方式 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);