adb.setTitle("Confirm?"); adb.setMessage("Plese Confirm"); adb.setPositiveButton("Cancel", new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { // Cancel Event } }); adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { // OK Event } }); adb.show();
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="35dp" android:text="Click" /> </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final AlertDialog.Builder adb = new AlertDialog.Builder(this); // Button1 final Button btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { adb.setTitle("Confirm?"); adb.setMessage("Plese Confirm"); adb.setNegativeButton("Cancel", null); adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int arg1) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"Your Click OK. " , Toast.LENGTH_LONG).show(); } }); adb.show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท