gpt4 book ai didi

java - 对话框中的取消按钮不关闭对话框

转载 作者:行者123 更新时间:2023-12-01 23:44:27 24 4
gpt4 key购买 nike

我有一个启动对话框:

// 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 代码,为上面的 fragment 提供更多上下文:

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


}







}

最佳答案

make final Dialog dial = null; 一个全局变量( Activity 级别),删除 final 并在您现在拥有的位置进行初始化,这样就可以了。

关于java - 对话框中的取消按钮不关闭对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17375546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com