Java AlertDialog Builder OnItemSelectedListener
我对AlertDialog Builder有一个棘手但烦人的问题。 我想在定制的AlertDialog中处理项目选择,但OnItemSelectedListener似乎没有选择我的点击 自定义对话框:Java AlertDialog Builder OnItemSelectedListener,java,android,android-alertdialog,Java,Android,Android Alertdialog,我对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"
<?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>
和处理程序:
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();
}
private void handleLanguageDialog(){
final AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setView(R.layout.dialog\u语言);
builder.setOnItemSelectedListener(新适配器视图)。
OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图、,
内部位置,长id){
SharedReferences首选项=首选项管理器
.GetDefaultSharedReferences(getApplicationContext());
Log.i(“info”,“pressed”+Integer.toString(位置));
restartInLocale(新的区域设置(preferences.getString(“区域设置”),“”);
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
builder.setNegativeButton(“取消”,新对话框界面
.OnClickListener(){
@凌驾
公共void onClick(DialogInterface,inti){
}
});
builder.create().show();
}
这是正确的方法还是我遗漏了什么
提前谢谢你 看起来有点不对劲。应该是这样的:
@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();
}
专用void handleLanguageDialog(){
ArrayList listItems=新的ArrayList();
列表项。添加(“英文”);
添加(“阿拉伯语”);
新建AlertDialog.Builder(此)
.setTitle(“标题”)
.setCancelable(错误)
.setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,listItems),
新建DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口对话框,int项){
}
}).setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface对话框,int-id){
//登录用户。。。
}
}).setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
public void onClick(DialogInterface对话框,int-id){
}
}).show();
}
尝试在列表/微调器视图中使用setOnItemClick listener来显示列表,而不是生成器本身。例如,如果使用微调器显示列表,请使用-
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) {
}
});
final Spinner Spinner=(Spinner)layout.findViewById(R.id.Spinner);spinner.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0、视图arg1、内部arg2、长arg3){
int item=spinner.getSelectedItemPosition();
指挥官(项目);
}
@凌驾
未选择公共无效(AdapterView arg0){
}
});
尝试以下操作:将click listener添加到对话框中的语言项中
onimselected()
应用于listview
或spinner
项目您的对话框布局Dialog\u语言
没有这些项目
请尝试以下代码:
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();
我不认为这是正确的方法,你让它变得非常复杂。如果我没有错,那么您正在尝试显示语言选择对话框,您应该遵循以下给定步骤
<string-array name="lang">
<item>English</item>
<item>عربى</item>
</string-array>
这是在警报对话框中显示语言列表的最简单方法,没有任何正面或负面按钮。按钮布局在哪里?当您单击按钮时,是否调用了ClickDrawer?是的,将调用该按钮并显示对话框。没有一个积极的按钮,它什么都做不了。尝试此操作尝试在列表/微调器视图上设置OnItemClick listener,无论您使用哪个视图来显示列表,而不是生成器本身。例如,如果您使用微调器来显示列表,请使用它,但我需要它不带正按钮。onClick应该在该对话框中选择的项目上触发。不清楚您的意思。用户按什么键激活此“选定项”?我看到布局中有这些onClick侦听器,但没有“onLanguageButtonClicked”方法。也许您正在寻找的是在活动中创建该方法并在其中运行代码。当用户点击这些东西时,就会调用这个方法。是的,我完全忘记了这个方法。它是这样工作的,但现在我无法处理该对话框。有什么想法吗^_^在对话框上调用dismise()。我的对话框片段应该是什么样子?这样就打开了一个空片段。只需替换您的handleLanguageDialog()。我喜欢它的简单性,但请再容忍我一次。我想在onClick事件(而不是侦听器)上触发它。我应该把方法放在哪里?在onClick里面,它说这是不允许的。对不起,我对这个问题还不太熟悉。你好,博安塔·洛努特,我已经编辑了我的答案,请检查一下。从onClick事件中调用handleLanguageDialog()方法并检查它。它正按照我所希望的方式工作,非常感谢!
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();
<string-array name="lang">
<item>English</item>
<item>عربى</item>
</string-array>
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();
}