2012-09-26 3 views
1

한 번 튜토리얼을 읽었지만 XBOX가 내 전화를 지워 버렸기 때문에 잃어 버렸습니다. 나는이 하나를위한 안드로이드 버전을 현재 버전에 대한 같은 버전 번호와 같은 응용 프로그램 정보를 알리는에 AlertDialog 상자를 열 수있는 MainActivity 버튼 등android에서 경고 대화 상자를 만드는 방법은 무엇입니까?

package com.apw.games.rpg.medieval; 

import android.app.*; 
import android.os.*; 
import android.view.*; 
import android.widget.*; 
import android.content.*; 
import android.util.*; 
import android.graphics.*; 

public class MainActivity extends Activity 
{ 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 

@Override public void onNothingSelected(AdapterView<?> parent) { 

    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu, menu); return true; } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle item selection 
    switch (item.getItemId()) { 


     case R.id.quit: 
     Intent intent = new Intent(this, Exit.class); 
     startActivity(intent); 
     return true; 
     case R.id.new_game: 
      Intent i = new Intent(this, New_Game.class); 
      startActivity(i); 
      return true; 
     case R.id.visit_site: 
      Intent inte = new Intent(this, Site.class); 
      startActivity(inte); 
      return true; 
     default: return super.onOptionsItemSelected(item); 

     }} 
+0

가능한 복제 [I 안드로이드에 경고 대화 상자를 표시하려면 어떻게합니까?] (http://stackoverflow.com/questions/2115758/how-do-i-display-an-alert-dialog-on- android) –

답변

6

먼저가있을 싶어

AlertDialog alertDialog = new AlertDialog.Builder(Main.this).create();

Main.this 내 활동의 컨텍스트이다 : 모두에 AlertDialog 타입의 객체를 declarate. 당신이처럼 대화 상자의 제목을 설정할 수 있습니다

alertDialog.setTitle("Title");

그리고 메시지 :

alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 

    //here you can add functions 

} }); 

:

alertDialog.setMessage("Your text");

다음 일을, 당신의 버튼 (들) 기능을 설정 그리고이 줄을 사용하여 AlertDialog의 아이콘을 바꿀 수 있습니다 : alertDialog.setIcon(R.drawable.icon);

마지막 것은, 당신의 대화 상자를 표시하는 것을 잊지 마세요 :

alertDialog.show();

2
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this) 
     .setTitle("alert dialog") 
     .setMessage("message") 

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

      public void onClick(DialogInterface dialog, int which) {        
       Activity.this.finish(); 

      } 
     }) 

     .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       Activity.this.finish(); 
      } 
     }) 
     .show();  
+0

오, 고마워! 그것이 나인 때 나는 받아 들일 것이다! – apw2012

+0

내가 말한대로 삽입했지만 경고 대화 상자 인스턴스를 사용할 수 없다는 메시지가 나타납니다. – apw2012

-1

위의 설명은 좋다. AlertDialog, Progress Dialog, DatePickerDialog, TimePickerDialog와 같은 클래스 유형에 기반한 네 가지 유형의 안드로이드 대화 상자가 있습니다. 우리는 필요에 따라 선택할 수 있습니다. 메시지를 표시하기위한 일반 대화 상자가 있다면 AlertDialog를 사용하십시오. 대화 상자를 만들기 위해 단계별 프로세스를 읽으려는 경우 how to create alert dialog box in android에 대한 간단한 예제를 살펴보십시오.

+0

실제 질문은 경고 대화 상자를 만드는 방법입니다. 대답은 해당 질문과 관련이 있어야합니다. –

1
// here is a snippet code work for me 
    new AlertDialog.Builder(this) 
    .setTitle("Mobile Raksha") 
    .setMessage(
      "Your Message") 
    .setCancelable(true) 
    .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface arg0, int arg1) { 
      arg0.dismiss(); 
      finish(); 
     } 
    }).show(); 
관련 문제