对于刚刚接触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();
}
});
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
Android AlertDialog具体使用方法就为大家介绍到这里。
【编辑推荐】