【发布时间】: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