package com.dialog;</p><p>import android.app.Activity;<br />import android.app.AlertDialog;<br />import android.app.Dialog;<br />import android.content.DialogInterface;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />import android.widget.LinearLayout;</p><p>/**<br /> * 對話 確認框 例子代碼<br /> * @author CRF<br /> *<br /> */<br />public class exeDialogComfirm extends Activity {<br /> /** Called when the activity is first created. */<br /> static final int DIALOG_PAUSED_ID = 0;</p><p> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> LinearLayout layout = new LinearLayout(this);</p><p> Button button = new Button(this);</p><p> button.setText("I'm So great Button");</p><p> button.setOnClickListener(new OnClickListener() {</p><p> public void onClick(View v) {</p><p> showDialog(DIALOG_PAUSED_ID);</p><p> }</p><p> });</p><p> layout.addView(button);</p><p> setContentView(layout);</p><p> }</p><p> @Override<br /> protected Dialog onCreateDialog(int id)</p><p> {</p><p> AlertDialog.Builder builder = new AlertDialog.Builder(this);</p><p> builder.setMessage("Are you sure you want to exit?")</p><p> .setCancelable(false)</p><p> .setPositiveButton("Yes", new DialogInterface.OnClickListener() {</p><p> public void onClick(DialogInterface dialog, int id) {</p><p> exeDialogComfirm.this.finish();</p><p> }</p><p> })</p><p> .setNegativeButton("No", new DialogInterface.OnClickListener() {</p><p> public void onClick(DialogInterface dialog, int id) {</p><p> dialog.cancel();</p><p> }</p><p> });</p><p> AlertDialog alert = builder.create();</p><p> return alert;</p><p> }</p><p>}</p><p>--------------------------------------------------------------------</p><p>例子二:提示資訊視窗例子</p><p>package com.dialog;</p><p>import android.app.Activity;<br />import android.content.Context;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />import android.widget.LinearLayout;<br />import android.widget.Toast;<br />/**<br /> * 點擊按鈕,提示資訊框例子<br /> * @author CRF<br /> *<br /> */<br />public class excDialog extends Activity {<br /> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> // setContentView(R.layout.main);</p><p> LinearLayout layout = new LinearLayout(this);</p><p> Button button = new Button(this);</p><p> button.setText("I'm So great Button");</p><p> button.setOnClickListener(new OnClickListener(){</p><p> public void onClick(View v) {<br /> //提示資訊顯示<br /> Context context = getApplicationContext();</p><p> CharSequence text = "Hello toast!";</p><p> int duration = Toast.LENGTH_SHORT;</p><p> Toast toast = Toast.makeText(context, text, duration);</p><p> toast.show();</p><p> }</p><p> });</p><p> layout.addView(button);</p><p> setContentView(layout);</p><p> }</p><p>}</p><p>