activity
代码package com.dialog.test1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Button;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
publicclass DialogTest1 extends Activity {
/** Called when the activity is first created. */
private Button btn;
private String TAG="DialogTest1";
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(mylistener);
}
private OnClickListener mylistener=new OnClickListener(){
@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
//按了button按钮以后,让他弹出一个对话框
new AlertDialog.Builder(DialogTest1.this)
.setIcon(R.drawable.icon)
.setTitle(R.string.title_name)
.setMessage(R.string.message_name)
.setPositiveButton(R.string.btn1_name, new DialogInterface.OnClickListener(){
@Override
publicvoid onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.v(TAG,"你点击了确定");
//activity finish
finish();
}
})
.show();
}
};
@Override
protectedvoid onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.v(TAG,"activity finish");
}
}main.xml代码<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="按我会显示对话框哦!"/>
</LinearLayout>
string.xml代码<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, DialogTest1!</string>
<string name="app_name">DialogTest1</string>
<string name="title_name">我是标题</string>
<string name="message_name">我是message</string>
<string name="btn1_name">确定</string>
<string name="btn2_name">取消</string>
</resources>log 信息
V/DialogTest1( 2244): 你点击了确定
V/DialogTest1( 2309): activity finish
如果不加finish这句话,点完确定之后,对话框也会消失。
http://www.cnblogs.com/snowdrop/articles/1893594.html