如何在对话框中实现旋转器的OnItemSelectedListener
我的 MainActivity
中有一个对话框,可以通过触摸布局中的齿轮按钮来激活该对话框。当它出现时,它包含一个下拉Spinner
。 Spinner
可以工作并生成一个可滚动的项目列表,所以这至少是好的。
尽管如此,我仍然坚持正确应用有效的 OnSpinnerItemSelectedListener
,我想对其进行自定义以便为所选的每个项目提供响应。
有人可以帮助我提供有关如何最好地应用侦听器并使其正常工作的建议吗?我们将非常感谢您的帮助!
在下面的代码中,我只包含了 MainActivity
中引用该框内的对话框和微调器的部分。如果您需要更多代码,请告诉我 - 我非常乐意提供更多信息! PowerSpinnerView
引用我正在使用的 Spinner
类型(来自在我的 Gradle 文件中实现的第三方依赖项)。
options = findViewById(R.id.optionscog);
options.setOnClickListener(new View.OnClickListener() {
private PowerSpinnerView spItems;
ArrayAdapter arrayAdapter;
@Override
public void onClick(View view) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.activity_options_menu);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(true);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
View back = dialog.findViewById(R.id.arrow);
PowerSpinnerView powerSpinnerView = (PowerSpinnerView)
dialog.findViewById(R.id.spItems);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
back.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Bitmap map = takeScreenShot(MainActivity.this);
Bitmap fast = fastblur(map, 50);
final Drawable draw=new BitmapDrawable(getResources(),fast);
dialog.getWindow().setBackgroundDrawable(draw);
dialog.getWindow().setAttributes(lp);
dialog.show();
}
});
I have a Dialog Box in my MainActivity
that is activated by touching a cog button in my Layout. When it appears, it contains a drop down Spinner
. The Spinner
works and produces a scrollable list of items, so that is good at least.
Albeit, I am stuck on correctly applying an OnSpinnerItemSelectedListener
that works, which I want to customise in order to provide responses for each item selected.
Is anybody able to help me by providing advice on how best to apply a Listener and get it to work please? Your help would be massively appreciated!
With the code below, I have only included the portion of my MainActivity
that references the Dialog Box and Spinner inside of that box. If you require further code, then please let me know - I'm more than happy to provide more info! PowerSpinnerView
references the type of Spinner
I'm using (from a third party dependency implemented in my Gradle file).
options = findViewById(R.id.optionscog);
options.setOnClickListener(new View.OnClickListener() {
private PowerSpinnerView spItems;
ArrayAdapter arrayAdapter;
@Override
public void onClick(View view) {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.activity_options_menu);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(true);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
View back = dialog.findViewById(R.id.arrow);
PowerSpinnerView powerSpinnerView = (PowerSpinnerView)
dialog.findViewById(R.id.spItems);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
back.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Bitmap map = takeScreenShot(MainActivity.this);
Bitmap fast = fastblur(map, 50);
final Drawable draw=new BitmapDrawable(getResources(),fast);
dialog.getWindow().setBackgroundDrawable(draw);
dialog.getWindow().setAttributes(lp);
dialog.show();
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用来自第 3 方的
PowerSpinnerView
,因此我查看了其文档 对于点击侦听器
:您选择的项目是
newItem
,其索引是new index
You use
PowerSpinnerView
from a 3rd party, so I looked at its documentation for theclick listener
:the item you select is
newItem
and its index isnew index
为了保存旋转器的选择,您需要的只是在XML文件上添加一个属性:
所有保存工作均由第三方库
For saving the spinner selection, all that you need is adding an attribute at the XML file:
all the saving work is done by the 3rd party library