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 に答える