Chinaunix

标题: 看代码学Android :如何显示对话框MessageBox? [打印本页]

作者: zdwang    时间: 2011-12-21 08:41
标题: 看代码学Android :如何显示对话框MessageBox?
  1. package com.test;

  2. //import java.net.URISyntaxException;

  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.Environment;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.Toast;

  10. public class TestActivity1 extends Activity {
  11.     /** Called when the activity is first created. */
  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.main);

  16.         Button btn = (Button) this.findViewById(R.id.button1);
  17.         btn.setOnClickListener(new View.OnClickListener() {
  18.             public void onClick(View v) {

  19.                 // Show a hint message box and auto disappear after timeout
  20.                 Toast.makeText(getApplicationContext(), "Hello Android, I am coming....",
  21.                      Toast.LENGTH_LONG).show();                 
  22.             }
  23.         });
  24.     }
  25. }


-----------------------------------------------------------------------------
搞个有按钮的对话框,点击button退出activity.
  1. Dialog dlg = new AlertDialog.Builder(TestActivity1 .this)
  2.                 .setMessage(R.string.hello)
  3.                 .setCancelable(false)   // disable cancel action, hardware back button 
  4.                 .setPositiveButton(R.string.OK,
  5.                         new DialogInterface.OnClickListener() {
  6.                                                         
  7.                             public void onClick(DialogInterface dialog, int which) {
  8.                                  TestActivity1.this.finish();
  9.                             }
  10.                             
  11.                         }).create();
  12.                 
  13.             
  14.             dlg.show();





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2