You are on page 1of 6

AlertDialog & Toast

Project : AlertDialogDemo

สําหรับโปรเจ็กนี้ จะแนะนําให้รู้จักกับ AlertDialog และ Toast


โดยที่จะให้ คลิกที่ปุ่ม แล้วแสดง AlertDialog หรือ Toast

<?xml version="1.0" encoding="utf-8"?>


<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/dialog"
android:layout_width = "200dip"
android:layout_height = "wrap_content"
android:layout_gravity = "center_horizontal"
android:layout_marginTop = "30dip"
android:text = "Show AlertDialog!" >
</Button>

<Button
android:id = "@+id/toast"
android:layout_width = "200dip"
android:layout_height = "wrap_content"
android:layout_gravity = "center_horizontal"
android:text = "Show Toast" >
</Button>

</LinearLayout>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

Code XML ในการจัด Layout ของ LinearLayout


orientation : อะไรก็ตามที่ใส่เข้าไปใน linear จะเรียงต่อกันตามแนวดิ่ง
width : กว้าง เท่ากับขนาดของ parent ในที่นี้ คือ จอภาพ
height : สูง เท่ากับขนาดของ parent ในที่นี้ คือ จอภาพ
<Button
android:id = "@+id/dialog"
android:layout_width = "200dip"
android:layout_height = "wrap_content"
android:layout_gravity = "center_horizontal"
android:layout_marginTop = "30dip"
android:text = "Show AlertDialog!" >
</Button>

Code XML ในการจัด Layout ของ Button


id : มีชื่อตัวแปรว่า dialog
width : กว้าง เท่ากับ 200 dip
height : สูง เท่ากับขนาดของข้อมูลที่อยู่ข้างใน ซึ่ง ในที่นี้คือ Show AlertDialog
gravity : เป็นการจัดให้ ปุ่มนี้ อยู่ตรงกลางของ LinearLayout
marginTop : คือให้ถูกดันลงมาจาก ด้านบน ขนาด 30 dip
text : เป็นการกําหนดตัวอักษรให้กับ ปุ่ม ในที่นี้คือ Show AlertDialog

<Button
android:id = "@+id/toast"
android:layout_width = "200dip"
android:layout_height = "wrap_content"
android:layout_gravity = "center_horizontal"
android:text = "Show Toast" >
</Button>

Code XML ในการจัด Layout ของ Button


id : มีชื่อตัวแปรว่า dialog
width : กว้าง เท่ากับ 200 dip
height : สูง เท่ากับขนาดของข้อมูลที่อยู่ข้างใน ซึ่ง ในที่นี้คือ Show Toast
gravity : เป็นการจัดให้ ปุ่มนี้ อยู่ตรงกลางของ LinearLayout
text : เป็นการกําหนดตัวอักษรให้กับ ปุ่ม ในที่นี้คือ Show Toast
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AlertDialogDemo extends Activity {



Button alertDialog,toast;
Context context = this;

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

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;

Code Java ในการประกาศตัวแปร Button และ Context

alertDialog = (Button)findViewById(R.id.dialog);

Code Java ในการการเชื่อม ตัวแปร Button กับ Layout ( XML )

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();
}
});

Code Java ในการการกําหนด เหตุการณ์ เมื่อมีการ คลิกที่ปุ่ม alertDialog


โดยที่เมื่อกดแล้ว จะแสดง AlertDialog
วิธีการ
1. ประกาศตัวแปร AlertDialog.Builder จะได้ว่า

AlertDialog.Builder alert = new AlertDialog.Builder(context);

2.กําหนด Title( ข้อความในส่วนหัวของ AlertDialog ) ในที่นี้ให้แสดงว่า Message จะได้ว่า

alert.setTitle("Message");
3.กําหนดข้อความที่จะแสดง ในที่นี้จะแสดงว่า This is AlertDialog จะได้ว่า

alert.setMessage("This is AlertDialog");

4.ใส่ปุ่มให้กับ AlertDialog ในที่นี้เราจะใช้แค่ปุ่มเดียว จึงเลือกใช้ NeutralButton


แล้ว กําหนดเหตุการณ์การคลิกปุ่มด้วย แต่ในที่นี้เราจะไม่ทําะไรกับการคลิก
เพียงแค่ให้ AlertDialog หายไปเมื่อคลิกเท่านั้น จะได้ว่า

alert.setNeutralButton("Close", new DialogInterface.OnClickListener() {


@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
});

5.แสดง AlertDialog จะได้ว่า

alert.show();

toast = (Button)findViewById(R.id.toast);

Code Java ในการการเชื่อม ตัวแปร Button กับ Layout ( XML )

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();
}
});

Code Java ในการการกําหนด เหตุการณ์ เมื่อมีการ คลิกที่ปุ่ม toast


โดยที่เมื่อกดแล้ว จะแสดง Toast

วิธีการ
1.สร้าง ToastMessage
กําหนด Context , ข้อความ , ระยะเวลาที่ให้แสดง Toast
จากนั้นก็ แสดง ToastMessage จะได้ว่า

Toast.makeText(context,"This is Toast",Toast.LENGTH_SHORT).show();

drbomkung@gmail.com

You might also like