0

我需要帮助实现代码以添加否定或肯定按钮以关闭我的警报对话框(任何帮助将不胜感激)。我认为我在代码中的一些标点符号也需要更改,所以任何帮助都会很棒:)

package kevin.erica.box;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.Random;

public class TheKevinAndEricaBoxActivity extends Activity {
/** Called when the activity is first created. */
private String[] myString;
private String list;
private String[] myString2;
private String list2;
private static final Random rgenerator = new Random();
private static final Random rgenerator2 = new Random();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Resources res = getResources();

    myString = res.getStringArray(R.array.myArray);

    list = myString[rgenerator.nextInt(myString.length)];

    myString2 = res.getStringArray(R.array.myArray2);

    list2 = myString2[rgenerator.nextInt(myString2.length)];

    ImageButton ib = (ImageButton) findViewById(R.id.imagebutton1);
    ib.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View erica) {
            AlertDialog.Builder b = new AlertDialog.Builder(
                    TheKevinAndEricaBoxActivity.this);
                  b.setMessage(myString[rgenerator.nextInt(myString.length)]);
            b.setTitle(R.string.title1); 
Dialog d = b.create();
            d.show();

        }
    });
}
}
4

5 回答 5

3

您可以在您的应用程序中使用以下代码::::

AlertDialog.Builder b = new AlertDialog.Builder(TheKevinAndEricaBoxActivity.this);
b.setMessage(myString[rgenerator.nextInt(myString.length)]);
b.setTitle(R.string.title1); 
b.setPositiveButton("Button Text", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
    //stuff you want the button to do
}
});
b.setNegativeButton("Button Text", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
    //stuff you want the button to do
}
});
于 2012-04-18T18:14:39.860 回答
2
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

            // set title
            alertDialogBuilder.setTitle("Your Title");

            // set dialog message
            alertDialogBuilder
                .setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity
                        MainActivity.this.finish();
                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

在发布问题之前在 Google 中搜索。

于 2012-04-18T18:16:12.737 回答
1

这是一个指南。

注意函数“ set_ _ _Button”。

于 2012-04-18T18:14:05.467 回答
0
b.setNegativeButton("Button Text", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
    //stuff you want the button to do
});
于 2012-04-18T18:13:17.323 回答
0

您需要制作一个自定义对话框。请参阅示例:

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");

TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

这是完整的示例:

http://developer.android.com/guide/topics/ui/dialogs.html

于 2012-04-18T18:15:26.603 回答