【问题标题】:AlertDialog Builder OnItemSelectedListener警报对话框生成器 OnItemSelectedListener
【发布时间】:2017-07-21 17:08:28
【问题描述】:

我有一个关于 AlertDialog Builder 的小而烦人的问题。 我想在定制的 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();
}

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

提前谢谢你!

【问题讨论】:

  • 您的按钮布局在哪里?当你点击按钮时 onClickDrawer 是否被调用?
  • 是的,它被调用并显示对话框。没有正面按钮,它就什么也做不了。
  • 尝试在列表/微调器视图上设置 OnItemClick 侦听器,无论您用于显示列表而不是构建器本身。就像您使用微调器来显示列表一样使用-

标签: java android android-alertdialog


【解决方案1】:

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

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

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

【讨论】:

  • 我喜欢简单,但请多多包涵。我想在 onClick 事件(不是侦听器)上触发它。我应该把方法放在哪里?在 onClick 里面它说这是不允许的。抱歉,但我对此真的很陌生。
  • 你好 Boanta lonut,我已经编辑了我的答案,只是检查一下。从 onClick 事件中调用 handleLanguageDialog() 方法并检查它。
  • 它的工作方式正是我想要的,非常感谢!
【解决方案2】:

看起来有点不对劲。它应该是这样的:

@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

【讨论】:

  • 它是这样工作的,但我需要它没有正面按钮。 onClick 应该在该对话框中选择的项目上触发。
  • 不清楚你的意思。用户按什么来激活这个“选定的项目”?
  • 我看到您在布局中有这些 onClick 侦听器,但没有“onLanguageButtonClicked”方法。也许您正在寻找的是在您的 Activity 中创建该方法并在其中运行代码。当用户点击这些东西时,该方法将被调用。
  • 是的,我完全忘记了那个方法。它以这种方式工作,但现在我无法处理对话框。有任何想法吗? ^_^
  • 在对话框中调用dismiss()。
【解决方案3】:
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();
}

【讨论】:

  • 我的对话片段应该是什么样子?它以这种方式打开一个空片段。
  • 只需替换您的 handleLanguageDialog()。
【解决方案4】:

尝试在列表/微调器视图上使用 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) {
        }
    });

【讨论】:

    【解决方案5】:

    试试这个:将点击监听器添加到对话框中的语言项

    onItemSelected() 应用于 listviewspinner 项目,您的对话框布局 dialog_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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-12
      • 2017-07-11
      • 1970-01-01
      相关资源
      最近更新 更多