如何在对话框中实现旋转器的OnItemSelectedListener

发布于 2025-01-19 00:13:14 字数 2170 浏览 3 评论 0原文

我的 MainActivity 中有一个对话框,可以通过触摸布局中的齿轮按钮来激活该对话框。当它出现时,它包含一个下拉SpinnerSpinner 可以工作并生成一个可滚动的项目列表,所以这至少是好的。

尽管如此,我仍然坚持正确应用有效的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红焚 2025-01-26 00:13:15

您使用来自第 3 方的 PowerSpinnerView,因此我查看了其文档 对于点击侦听器

powerSpinnerView.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener<String>() {
 @Override public void onItemSelected(int oldIndex, @Nullable String oldItem, int newIndex, String newItem) {
   // do whatever you need here 
 }
});

您选择的项目是newItem,其索引是new index

You use PowerSpinnerView from a 3rd party, so I looked at its documentation for the click listener:

powerSpinnerView.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener<String>() {
 @Override public void onItemSelected(int oldIndex, @Nullable String oldItem, int newIndex, String newItem) {
   // do whatever you need here 
 }
});

the item you select is newItem and its index is new index

难理解 2025-01-26 00:13:15

为了保存旋转器的选择,您需要的只是在XML文件上添加一个属性:

app:spinner_preference_name="spinner_chs"

所有保存工作均由第三方库

For saving the spinner selection, all that you need is adding an attribute at the XML file:

app:spinner_preference_name="spinner_chs"

all the saving work is done by the 3rd party library

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文