对于刚刚接触Android这一手机操作系统的朋友来说,可能还对其中一些重要的功能不是很清楚,以及具体应用技巧掌握不牢。在这里我们就来通过一段代码的解读,为大家详细介绍Android AlertDialog的使用方法。
Android AlertDialog代码示例:
- 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();
- }
- });
- }
- }
Android AlertDialog具体使用方法就为大家介绍到这里。
【编辑推荐】