2013-01-23 59 views
0

我已經在android.now中做了一個簡單的警告框的程序,我必須把兩個按鈕「確定」和「取消」,但是當我運行該程序時,它只顯示「取消「按鈕...我的代碼如下:如何把兩個按鈕放在警戒框

Main.java

public class MainActivity extends Activity { 
Button btn; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 
    btn = (Button)findViewById(R.id.button); 
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
    btn.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     alertDialog.setTitle("Title"); 
     alertDialog.setMessage("Message"); 
     alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Add your code for the button here. 
       Toast.makeText(getApplicationContext(), "well come", 1).show(); 
      } 
     }); 
     alertDialog.setButton("cancel",new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show(); 
      } 
     }); 
     // Set the Icon for the Dialog 
     alertDialog.setIcon(R.drawable.ic_launcher); 
     alertDialog.show(); 
     // see http://androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button 
    } 
}); 
    } 
} 

預先感謝您。

+0

請仔細閱讀此代碼:http://www.mkyong.com/android/android-alert-dialog-example/ – krishna

回答

4

我有同樣的問題。這就是做了什麼。

public class MainActivity extends Activity { 
    Button btn; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 
     btn = (Button)findViewById(R.id.button); 
     final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
     btn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      alertDialog.setTitle("Title"); 
      alertDialog.setMessage("Message"); 
      alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Toast.makeText(getApplicationContext(), "well come", 1).show(); 
       } 
      }); 
     alertDialog.setButton2("cancel",new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show(); 
      } 
     }); 
     // Set the Icon for the Dialog 
     alertDialog.setIcon(R.drawable.ic_launcher); 
     alertDialog.show(); 
     // see http://androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button 
    } 
}); 
    } 
} 

這工作正常。您必須爲警報框的按鈕編號。

1

像這樣改變我們安排setPositiveButtonoksetNegativeButtoncancel按鈕。

final AlertDialog.Builder alertDialog= new AlertDialog.Builder(this); 

alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Add your code for the button here. 
      Toast.makeText(getApplicationContext(), "well come", 1).show(); 
     } 
    }); 
    alertDialog.setNegativeButton("cancel",new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show(); 
     } 
    }); 
+0

@ nagarjuna-Thanx brothr ...! –

+0

you welcome ......... – NagarjunaReddy

1

正確的方式來增加按鈕:

alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog,int id) { 
     // perform your action 
    } 
}); 

alertDialog.setNegativeButton("Cancel",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(); 
    } 
}); 

Android Alert Dialog Example

+0

@ Robinhood-Thnx ,, ...!robin –

0
public void showDialog(Activity activity, String title, CharSequence message) { 
AlertDialog.Builder builder = new AlertDialog.Builder(activity); 
if (title != null) 
    builder.setTitle(title); 
builder.setMessage(message); 
builder.setPositiveButton("OK", null); 
     builder.setNegativeButton("Cancel", null); 
builder.show(); 

}

+0

-Thanks ....親愛的..!它的工作..!有一個Nyc日..! :) –

1

簡單警報

private AlertDialog AskOption() 
{ 
    AlertDialog myQuittingDialogBox =new AlertDialog.Builder(this) 
     //set message, title, and icon 
     .setTitle("Title") 
     .setMessage("Message") 
     .setIcon(R.drawable.icon) 

     .setPositiveButton("yes", new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface dialog, int whichButton) { 

       //your code 
      } 
     }) 

     .setNeutralButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 

       dialog.dismiss(); 
     } 
     }) 

     .create(); 
     return myQuittingDialogBox; 

    } 

使用

AlertDialog al = AskOption(); 
al.show(); 
0

你需要使用setPositiveButton()和setnegativeButton()一樣,

import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
Button btn; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 
    btn = (Button)findViewById(R.id.button); 
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
btn.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     alertDialog.setTitle("Title"); 
     alertDialog.setMessage("Message"); 
     alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, close 
        // current activity 
        Toast.makeText(getApplicationContext(), "well come", 1).show(); 
       } 
       }); 
     alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        // if this button is clicked, just close 
        // the dialog box and do nothing 
        Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show(); 
       } 
      }); 
     // Set the Icon for the Dialog 
     alertDialog.setIcon(R.drawable.ic_launcher); 
     alertDialog.show(); 
     // see http://androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button 
    } 
}); 
    } 
} 
+0

@ ajit-tnx bro ..! –