-1

这是我的活动,其中我有对话框的代码......我想做的是,当用户按下按钮时,必须出现一个对话框,询问用户是否要将该链接保存在数据库中。

public class Add_ink extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.websearch);

    Button btn = (Button)  findViewById(R.id.add_link);
    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
               showDialog(0);           
        }
    });
    }
    @Override
    protected Dialog onCreateDialog(int id){
         switch (id){
         case 0:
        return new AlertDialog.Builder(this)
         .setIcon(R.drawable.help_ov)
         .setTitle("This is a dialog with simple text")
         .setPositiveButton("OK",new DialogInterface.OnClickListener() {


            public void onClick(DialogInterface dialog, int whichButton) {
                Toast.makeText(getBaseContext(), "OK Clicked", Toast.LENGTH_SHORT).show();              
            }
        })
        .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {


            public void onClick(DialogInterface dialog, int whichButton) {
                Toast.makeText(getBaseContext(), "Cancel Clicked", Toast.LENGTH_SHORT).show();

            }
        })


        .create(); 
         }
        return null;


    }   
}
4

1 回答 1

0
Hi The Solutions is 


/*Code*/

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.websearch);

Button btn = (Button)  findViewById(R.id.add_link);
btn.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
           showDialog();           
    }
});

public void showDialog()
{
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Do you really want to finish ?? You haven't selected any security settings option to activate your app.");  

    builder.setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(
                                    @SuppressWarnings("unused") final DialogInterface dialog,
                                    @SuppressWarnings("unused") final int id) {

                     Toast.makeText(getBaseContext(), "OK Clicked", Toast.LENGTH_SHORT).show();         
dialog.cancel();        
                            }
                        })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog,
                            @SuppressWarnings("unused") final int id) {

Toast.makeText(getBaseContext(), "Cancel Clicked", Toast.LENGTH_SHORT).show();
                        dialog.cancel();

                    }
                });
        final AlertDialog alert = builder.create();
        alert.show();


}
于 2012-09-16T15:24:11.140 回答