首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AlertDialog Builder OnItemSelectedListener

AlertDialog Builder OnItemSelectedListener
EN

Stack Overflow用户
提问于 2017-03-02 05:30:57
回答 5查看 2.2K关注 0票数 3

我在AlertDialog生成器上有一个令人讨厌的问题。我想在定制的AlertDialog中处理项目选择,但是OnItemSelectedListener似乎不选择我的点击。

自定义对话框:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="120dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp"
android:background="#AAFFFFFF"
android:orientation="vertical">


<TextView
    android:text="English"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:id="@+id/lang_english"
    android:gravity="center_vertical"
    android:background="@color/borders"
    android:paddingLeft="10dp"
    android:onClick="onLanguageButtonClicked" />
<View
    android:layout_height="1dp"
    android:layout_width="match_parent"
    android:background="#000000" />

<TextView
    android:text="عربى"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:id="@+id/lang_arabic"
    android:gravity="center_vertical"
    android:background="@color/borders"
    android:paddingRight="10dp"
    android:onClick="onLanguageButtonClicked" />

<View
    android:layout_height="1dp"
    android:layout_width="match_parent"
    android:background="#2e2e2e"/>

<TextView
    android:text="کوردی "
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:id="@+id/lang_kurdish"
    android:gravity="center_vertical"
    android:background="@color/borders"
    android:paddingRight="10dp"
    android:onClick="onLanguageButtonClicked" />

</LinearLayout>

在按钮单击我打开对话框并处理它:

代码语言:javascript
复制
public void onClickDrawer(View view) {
    switch (view.getId()) {
        case R.id.button_account_language:
            handleLanguageDialog();
            break;
        case R.id.button_account_currency:
            handleCurrencyDialog();
            break;
        default:
            break;
    }
}

处理人员:

代码语言:javascript
复制
private void handleLanguageDialog() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(R.layout.dialog_language);
    builder.setOnItemSelectedListener(new      AdapterView.
    OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, 
        int position, long id) {
            SharedPreferences preferences =  PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
            Log.i("info", "pressed" + Integer.toString(position));
            restartInLocale(new Locale(preferences.getString("locale","")));
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    builder.setNegativeButton("CANCEL", new   DialogInterface
    .OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
        });
    builder.create().show();
}

这是正确的做法还是我遗漏了什么?

提前谢谢你!

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2017-03-02 06:05:53

我不认为这是正确的方法,你正在使它变得非常复杂。如果我没有错,那么您正在尝试显示语言选择对话框,因此您应该遵循以下步骤。

  1. 在strings.xml资源文件中创建字符串数组 英语عربى
  2. 当您想要显示警报对话框时,请在下面写代码 私有void handleLanguageDialog() { AlertDialog.Builder builder =新AlertDialog.Builder(getActivity());builder.setTitle(R.string.pick_color) .setItems(R.array.lang,新DialogInterface.OnClickListener() ){ public DialogInterface.OnClickListener onClick(DialogInterface对话框,int哪个){ //‘哪个“参数包含所选项开关(其中){ case 0:// >的索引位置//;);返回builder.create();}

这是在警报对话框中显示语言列表的最简单方法,没有任何正负按钮。

票数 4
EN

Stack Overflow用户

发布于 2017-03-02 05:37:41

看上去有点不对劲。它应该是这样的:

代码语言:javascript
复制
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
        // Add action buttons
       .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int id) {
               // sign in the user ...
           }
       })
       .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               LoginDialogFragment.this.getDialog().cancel();
           }
       });
    return builder.create();
}

https://developer.android.com/guide/topics/ui/dialogs.html

票数 1
EN

Stack Overflow用户

发布于 2017-03-02 05:40:43

代码语言:javascript
复制
private void handleLanguageDialog() {
    ArrayList<String> listItems = new ArrayList<>();
    listItems.add("English");
    listItems.add("Arabic");
    new AlertDialog.Builder(this)
            .setTitle("title")
            .setCancelable(false)
            .setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int item) {

                        }
                    }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            // sign in the user ...
        }
    }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

        }
    }).show();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42547549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档