2012-04-18 66 views
0

我需要幫助實施代碼才能添加否定或肯定按鈕來關閉我的提醒對話框(任何幫助都將不勝感激)。我想我的一些代碼標點符號的需求改變以及因此任何幫助將是巨大的作用「SET_ _ _Button」 :)如何在Android中向此提醒對話框添加關閉按鈕

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

     } 
    }); 
} 
} 

回答

2

您可以在應用程序中使用下面的代碼::::

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 
} 
}); 
+0

每一個代碼事務所PICE我能找到試圖實現給我大量的錯誤,如果我可以從.setnegativebutton位刪除錯誤它錯誤d.show位 – CarbonAssassin 2012-04-18 18:29:55

+0

導入android.content.DialogInterface;添加這些導入並嘗試。但似乎你已經導入 – 2012-04-18 18:35:26

+0

也編輯我的代碼有點嘗試現在最近大括號錯了,現在它更正 – 2012-04-18 18:37:30

0
b.setNegativeButton("Button Text", new DialogInterface.OnClickListener(){ 
@Override 
public void onClick(DialogInterface dialog, int which){ 
    //stuff you want the button to do 
}); 
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

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

在谷歌,你張貼問題之前,搜索..

+0

我有很多次,一直在努力工作一天。但是當我試圖實現它時,它給了我錯誤,並開始錯誤地將我的代碼的其他部分錯誤 – CarbonAssassin 2012-04-18 18:20:00

相關問題