我有一个启动对话框:
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.add_taste_dialog);
dialog.setTitle("Add Taste");
Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) dialog.findViewById(R.id.spinner2);
Button dialogButton = (Button) dialog.findViewById(R.id.cancelButton);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
问题是当我单击按钮时它不会关闭对话框。
我的对话框 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:id="@+id/tastePickTitle"
android:text="Select a Taste: "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:textStyle = "bold"
android:padding="5dip"
>
</TextView>
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/taste_array"
/>
<View
android:layout_width="fill_parent"
android:layout_height="30dp">
</View>
<TextView
android:id="@+id/ammountPickTitle"
android:text="How much taste: "
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:textStyle = "bold"
android:padding="5dip"
>
</TextView>
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/ammount_array"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_weight="1"
/>
<Button
android:id="@+id/addTasteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_weight="1"
android:onClick="addTasteNow" />
</LinearLayout>
</LinearLayout>
这是完整的标签 java 代码,为上面的片段提供更多上下文:
public class TasteTags extends Activity {
BeerData e;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tastetag_page);
//get beer data
Intent intent = getIntent();
Bundle b = intent.getExtras();
e = b.getParcelable("myBeerObject");
TextView beerTitle = (TextView) findViewById(R.id.beerTitleTaste);
beerTitle.setText(e.beerName + " Taste Profile");
String url = "myURL";
url = url + "b=" +e.beerId;
//async task to get beer taste tag percents
new GetTasteJSON(this).execute(url);
}
public void addTaste(View v){
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.add_taste_dialog);
dialog.setTitle("Add Taste");
Spinner spinner = (Spinner) dialog.findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) dialog.findViewById(R.id.spinner2);
Button dialogButton = (Button) dialog.findViewById(R.id.cancelButton);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("dialog", "cancel pressed");
dialog.dismiss();
}
});
dialog.show();
}
}