2017-03-07 1 views
0

내 IDE는 Android Studio 2.3입니다. 그리고이 코드에서 무엇이 잘못되었는지 이해하지 못합니다. 많은 문서를 언급했지만 답변을 찾을 수 없습니다. 나는이 내가 사용하는 코드는 현재 단지 초보자 입니다 :버튼 클릭시 AlertDialog를주는 방법

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 


     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    public class MyActivity extends Activity { 
     protected void onCreate(Bundle icicle) { 
      super.onCreate(icicle); 
      setContentView(R.layout.content_layout_id); 
      final Button button = (Button) findViewById(R.id.button); 
      button.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
          MainActivity.this); 
        alertDialogBuilder 
          .setTitle("Are you sure?") 
          .setCancelable(false) 
          .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            //TODO: Do something with parameter. 
           } 
          }) 
          .setNegativeButton("Cancel", 
            new DialogInterface.OnClickListener() { 
             public void onClick(DialogInterface dialog, int id) { 
              dialog.cancel(); 

             } 
            }); 

        // create alert dialog 
        AlertDialog alertDialog = alertDialogBuilder.create(); 

        // show it 
        alertDialog.show(); 
           } 
          }); 
        // Perform action on click 
       } 
      } 
     } 

답변

0

이 코드를보십시오 :

public void dialogBox() { 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
     alertDialogBuilder.setMessage("Click on Image for tag"); 
     alertDialogBuilder.setPositiveButton("Ok", 
      new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface arg0, int arg1) { 
      } 
     }); 

     alertDialogBuilder.setNegativeButton("cancel", 
      new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface arg0, int arg1) { 

      } 
     }); 

     AlertDialog alertDialog = alertDialogBuilder.create(); 
     alertDialog.show(); 
    } 
0

은 다음과 같이하십시오. 버튼을

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

     // Setting Dialog Title 
     alertDialog.setTitle("Confirm Delete..."); 

     // Setting Dialog Message 
     alertDialog.setMessage("Are you sure you want delete this?"); 

     // Setting Icon to Dialog 
     alertDialog.setIcon(R.drawable.delete); 

     // Setting Positive "Yes" Button 
     alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int which) { 

      // Write your code here to invoke YES event 
      Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     // Setting Negative "NO" Button 
     alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
      // Write your code here to invoke NO event 
      Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show(); 
      dialog.cancel(); 
      } 
     }); 

     // Showing Alert Message 
     alertDialog.show(); 

예는 여기를 클릭 : 그것은 오류가 표시 http://www.androidhive.info/2011/09/how-to-show-alert-dialog-in-android/

+0

: 어떻게해야합니까 AlertDialogActivity –

+0

를 해결할 수 없습니까? –