AlertDialog Builder OnItemSelectedListener

时间:2017-03-02 05:30:58

标签: java android alertdialog

我有一个使用AlertDialog Builder的tyny但烦人的问题。 我想在自定义的AlertDialog中处理项目选择,但是OnItemSelectedListener似乎没有选择我的点击。

自定义对话框:

<?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>

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

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;
    }
}

处理程序:

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();
}

这是正确的做法还是我错过了什么?

提前谢谢!

5 个答案:

答案 0 :(得分:3)

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

  1. 在strings.xml资源文件中创建字符串数组

    <string-array name="lang">
        <item>English</item>
        <item>عربى</item>
    </string-array>
    
  2. 如果要显示警告对话框,请记下代码

    private void handleLanguageDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.pick_color)
       .setItems(R.array.lang, new DialogInterface.OnClickListener()                 {
           public void onClick(DialogInterface dialog, int which) {
           // The 'which' argument contains the index position
           // of the selected item
           switch(which)
           {
              case 0:// English
                  break;
              case 1:// عربى
                  break;
           }
       }
    });
    return builder.create();
    }
    
  3. 这是在警告对话框中显示语言列表且没有任何正面或负面按钮的最简单方法。

答案 1 :(得分:1)

看起来有点偏。它应该是这样的:

@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

答案 2 :(得分:1)

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();
}

答案 3 :(得分:1)

尝试在列表/微调器视图上使用setOnItemClick侦听器,无论您使用哪个来显示列表而不是构建器本身。就像你使用微调器来显示列表一样 -

final Spinner spinner = (Spinner) layout.findViewById(R.id.spinner);                                                                       spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
            int item = spinner.getSelectedItemPosition();
            commandWriter(item); 
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });

答案 4 :(得分:1)

尝试此操作:将Click侦听器添加到对话框中的语言项

onItemSelected()适用于您的对话框布局listview没有的spinnerdialog_language项。

试试这段代码:

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
                // Get the layout inflater
                LayoutInflater inflater =getLayoutInflater();
                View myview = inflater.inflate(R.layout.dialog_language, null);
                // Inflate and set the layout for the dialog
                builder.setView(myview);

                TextView english = (TextView) myview.findViewById (R.id.lang_english);
                english.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        //do some stuff
                         SharedPreferences preferences =  PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                        Log.i("info", "pressed" + Integer.toString(position));
                        restartInLocale(new Locale(preferences.getString("locale","")));
                        }
                });

                TextView arabic = (TextView) myview.findViewById (R.id.lang_arabic);
                arabic.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                       //do other stuff
                    }
                });


                // Add action buttons
                       builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                               dialog.cancel();
                           }
                       });
                   builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           dialog.cancel();
                       }
                   });   

            builder.show();