Professional Documents
Culture Documents
8 - AlertDialog and Toast PDF
8 - AlertDialog and Toast PDF
Project : AlertDialogDemo
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id = "@+id/toast"
android:layout_width = "200dip"
android:layout_height = "wrap_content"
android:layout_gravity = "center_horizontal"
android:text = "Show Toast" >
</Button>
alertDialog = (Button)findViewById(R.id.dialog);
alertDialog.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Message");
alert.setMessage("This is AlertDialog");
alert.setNeutralButton("Close", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
});
alert.show();
}
});
toast = (Button)findViewById(R.id.toast);
toast.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context,"This is Toast",Toast.LENGTH_SHORT)
.show();
}
});
}
}
Button alertDialog,toast;
Context context = this;
alertDialog = (Button)findViewById(R.id.dialog);
alert.setTitle("Message");
3.กําหนดข้อความที่จะแสดง ในที่นี้จะแสดงว่า This is AlertDialog จะได้ว่า
alert.setMessage("This is AlertDialog");
alert.show();
toast = (Button)findViewById(R.id.toast);
drbomkung@gmail.com