2016-09-20 3 views
0
  case R.id.Delete_Contact: 

      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage(R.string.deleteContact) 
        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          mydb.deleteContact(id_To_Update); 
          Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show(); 
          Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
          startActivity(intent); 
         } 
        }) 
        .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          // User cancelled the dialog 
         } 
        }); 
      AlertDialog d = builder.create(); 
      d.setTitle("Are you sure"); 
      d.show(); 

      return true; 
     default: 
      return super.onOptionsItemSelected(item); 

これをボタンクリックイベントで使用するにはどうすればよいですか?このメニューボタンをクリックすると作品にメニューボタンを通常のボタンに変更する

+0

'Button'に' View.OnClickListener'を設定し、 'onClick'に同じコードを使用することで – 0xDEADC0DE

答えて

2

//あなたのボタンを初期化し

//あなたは同じコードが実行されるようにしたいとあなたは一般的な方法を使用する必要がありますよう

button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      //Perform Some Action 
      //common method to be called 
      deleteConfirmation(); 
}); 

public void deleteConfirmation(){ 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage(R.string.deleteContact) 
       .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         mydb.deleteContact(id_To_Update); 
         Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show(); 
         Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
         startActivity(intent); 
        } 
       }) 
       .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // User cancelled the dialog 
        } 
       }); 
     AlertDialog d = builder.create(); 
     d.setTitle("Are you sure"); 
     d.show(); 

     } 
    }); 

} 

下回ることにリスナーをクリックして設定します複数回。これは単なる提案です

関連する問題