Android AlertDialog 使用方法

发布时间:2011年06月17日      浏览次数:290 次
看看这个程序,基本就会使用它了。

package maximyudin.AlertDialogBuilderSample;


import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

import android.view.View;

import android.app.AlertDialog;

import android.content.DialogInterface;


public class AlertDialogBuilderSample extends Activity {

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main);


final Button btnQuit = (Button) findViewById(R.id.btnQuit);

btnQuit.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

new AlertDialog.Builder(AlertDialogBuilderSample.this)

.setTitle(“Question”)

.setMessage(“Are you sure that you want to quit?”)

.setIcon(R.drawable.question)

.setPositiveButton(“Yes”, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

setResult(RESULT_OK);

finish();

}

})

.setNegativeButton(“No”, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {


}

})

.show();

}

});



final Button btnTravels = (Button) findViewById(R.id.btnTravels);

btnTravels.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

new AlertDialog.Builder(AlertDialogBuilderSample.this)

.setTitle(“I want to go to”)

.setItems(R.array.items_indide_dialog,

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichcountry) {

String[] travelcountries =

getResources().getStringArray(R.array.items_indide_dialog);


new AlertDialog.Builder(AlertDialogBuilderSample.this)

.setMessage(“I’m going to “ + travelcountries[whichcountry])

.setNeutralButton(“Cancel”,

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton)

{


}

})

.show();

}

})

.show();

}

});

}

}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!