我正在构建一个Android应用程序,只需单击一个按钮,就会打开一个Alert对话框,其中有一个微调器和一个按钮。
如何在警报对话框中显示的按钮上实现onClick功能?
以下是我的警报对话代码:
private void viewCategory() {
AlertDialog.Builder viewDialog = new AlertDialog.Builder(getActivity());
viewDialog.setTitle("Host Your Game");
LayoutInflater li = (LayoutInflater)
getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = li.inflate(R.layout.customealertdialogbox, null);
viewDialog.setView(dialogView);
viewDialog.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
viewDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
viewDialog.show();
Spinner spinnercategory = (Spinner) dialogView
.findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getActivity(), R.array.SportSelection, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory.setAdapter(adapter);
Spinner spinnercategory2 = (Spinner) dialogView
.findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
getActivity(), R.array.time, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory2.setAdapter(adapter2);
Spinner spinnercategory3 = (Spinner) dialogView
.findViewById(R.id.spinner3);
ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(
getActivity(), R.array.Noofplayer, android.R.layout.simple_spinner_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory3.setAdapter(adapter3);
Spinner spinnercategory4 = (Spinner) dialogView
.findViewById(R.id.spinner4);
ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(
getActivity(), R.array.requirement, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory4.setAdapter(adapter4);
spinnercategory.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3) {
String selItem = parent.getSelectedItem().toString();
Toast.makeText(getActivity(), selItem,
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
这是此布局的Xml代码 -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Your Sport :"
android:layout_marginLeft="11sp"
android:layout_marginTop="5sp"
android:textSize="20sp"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sport Date :"
android:layout_marginLeft="11sp"
android:textSize="20sp"
/>
<DatePicker
android:id="@+id/datePicker1"
android:calendarViewShown="false"
android:layout_width="match_parent"
android:layout_height="150sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Your Time_Slot :"
android:layout_marginLeft="11sp"
android:textSize="20sp"
/>
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Max. No. Player in Sport :"
android:layout_marginLeft="11sp"
android:textSize="20sp"
/>
<Spinner
android:id="@+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Min. No. Player in Sport :"
android:layout_marginLeft="11sp"
android:textSize="20sp"
/>
<Spinner
android:id="@+id/spinner4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
答案 0 :(得分:0)
在这些线之后...
View dialogView = li.inflate(R.layout.customealertdialogbox, null);
viewDialog.setView(dialogView);
...添加
final Button button1=(Button)dialogView.findViewById(R.id.button1);//button1 is your button-id
然后使用此对象...并做任何你喜欢的事情
button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//your logic
}
});
答案 1 :(得分:0)
首先你必须在显示警告对话框之前初始化按钮并设置点击监听器:
final Button button1 = (Button) dialogView.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "button1 click",Toast.LENGTH_LONG).show();
}
});
已编入代码:
private void viewCategory(final Context context) {
AlertDialog.Builder viewDialog = new AlertDialog.Builder(context);
viewDialog.setTitle("Host Your Game");
View dialogView = LayoutInflater.from(context).inflate(R.layout.customealertdialogbox, null);
viewDialog.setView(dialogView);
final Spinner spinnercategory = (Spinner) dialogView.findViewById(R.id.spinner1);
final Spinner spinnercategory2 = (Spinner) dialogView.findViewById(R.id.spinner2);
final Spinner spinnercategory3 = (Spinner) dialogView.findViewById(R.id.spinner3);
final Spinner spinnercategory4 = (Spinner) dialogView.findViewById(R.id.spinner4);
final Button button1 = (Button) dialogView.findViewById(R.id.button1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.SportSelection, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory.setAdapter(adapter);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(context, R.array.time, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory2.setAdapter(adapter2);
ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(context, R.array.Noofplayer, android.R.layout.simple_spinner_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory3.setAdapter(adapter3);
ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(context, R.array.requirement, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory4.setAdapter(adapter4);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "button1 click",Toast.LENGTH_LONG).show();
}
});
spinnercategory.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View arg1,int arg2, long arg3) {
Toast.makeText(context, spinnercategory.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
viewDialog.show();
}