Dialog01Activity.java
003 | import android.app.Activity; |
004 | import android.app.AlertDialog; |
005 | import android.app.ProgressDialog; |
006 | import android.content.DialogInterface; |
007 | import android.content.Intent; |
008 | import android.os.Bundle; |
009 | import android.view.LayoutInflater; |
010 | import android.view.View; |
011 | import android.view.View.OnClickListener; |
012 | import android.widget.Button; |
013 | import android.widget.Toast; |
015 | public class Dialog01Activity extends Activity { |
017 | private Button button1,button2,button3,button4,button6,button7; |
018 | ProgressDialog progressDialog= null ; |
019 | public void onCreate(Bundle savedInstanceState) { |
020 | super .onCreate(savedInstanceState); |
021 | setContentView(R.layout.main); |
022 | button1=(Button)findViewById(R.id.button1); |
023 | button2=(Button)findViewById(R.id.button2); |
024 | button3=(Button)findViewById(R.id.button3); |
025 | button4=(Button)findViewById(R.id.button4); |
026 | button6=(Button)findViewById(R.id.button6); |
027 | button7=(Button)findViewById(R.id.button7); |
029 | button1.setOnClickListener( new ButtonListener()); |
030 | button2.setOnClickListener( new ButtonListener()); |
031 | button3.setOnClickListener( new ButtonListener()); |
032 | button4.setOnClickListener( new ButtonListener()); |
033 | button6.setOnClickListener( new ButtonListener()); |
034 | button7.setOnClickListener( new ButtonListener()); |
037 | class ButtonListener implements OnClickListener{ |
040 | public void onClick(View v) { |
041 | final String[] itemStrings={ "AA" , "BB" , "CC" , "DD" }; |
042 | // TODO Auto-generated method stub |
045 | AlertDialog.Builder dialog= new AlertDialog.Builder(Dialog01Activity. this ); |
046 | dialog.setTitle( "Dialog" ).setIcon(android.R.drawable.ic_dialog_info).setMessage( "弹出框" ).setPositiveButton( "确定" , new DialogInterface.OnClickListener() { |
049 | public void onClick(DialogInterface dialog, int which) { |
051 | // TODO Auto-generated method stub |
052 | Intent intent= new Intent(); |
053 | intent.setClass(getApplicationContext(), list. class ); |
054 | startActivity(intent); |
056 | }).setNegativeButton( "取消" , new DialogInterface.OnClickListener() { |
059 | public void onClick(DialogInterface dialog, int which) { |
060 | // TODO Auto-generated method stub |
061 | dialog.cancel(); //取消弹出框 |
067 | AlertDialog.Builder builder= new AlertDialog.Builder(Dialog01Activity. this ); |
068 | builder.setTitle( "LIST" ).setIcon(android.R.drawable.ic_lock_lock).setItems(itemStrings, new DialogInterface.OnClickListener() { |
071 | public void onClick(DialogInterface dialog, int which) { |
072 | // TODO Auto-generated method stub |
073 | Toast.makeText(getApplicationContext(), "你点击的是" +itemStrings[which], Toast.LENGTH_LONG).show(); |
079 | AlertDialog.Builder builder1= new AlertDialog.Builder(Dialog01Activity. this ); |
080 | builder1.setTitle( "LIST" ).setIcon(android.R.drawable.ic_lock_lock).setSingleChoiceItems(itemStrings,- 1 , new DialogInterface.OnClickListener() { |
083 | public void onClick(DialogInterface dialog, int which) { |
084 | // TODO Auto-generated method stub |
085 | Toast.makeText(getApplicationContext(), "你点击的是" +itemStrings[which], Toast.LENGTH_LONG).show(); |
089 | builder1.setCancelable( true ); |
092 | progressDialog=ProgressDialog.show(Dialog01Activity. this , "下载" , "下载中....." , true ); |
093 | progressDialog.setCancelable( true ); //当点击按钮返回的时候Dialog消失 |
094 | //progressDialog.dismiss(); |
098 | LayoutInflater inflater=(LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); |
099 | View view=inflater.inflate(R.layout.style, null ); |
100 | AlertDialog.Builder builder2= new AlertDialog.Builder(Dialog01Activity. this ); |
101 | builder2.setView(view); |
102 | builder2.setTitle( "QQ2011" ).setPositiveButton( "确定" , new DialogInterface.OnClickListener() { |
105 | public void onClick(DialogInterface dialog, int which) { |
106 | // TODO Auto-generated method stub |
113 | Dialog01Activity. this .finish(); |
main.xml布局
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:orientation = "vertical" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" |
06 | android:weightSum = "1" > |
07 | < Button android:id = "@+id/button1" android:layout_height = "wrap_content" android:layout_width = "match_parent" android:text = "简单弹出框" ></ Button > |
08 | < Button android:id = "@+id/button2" android:layout_height = "wrap_content" android:layout_width = "match_parent" android:text = "带有列表风格" ></ Button > |
09 | < Button android:id = "@+id/button3" android:layout_height = "wrap_content" android:layout_width = "match_parent" android:text = "带有Radio" ></ Button > |
10 | < Button android:id = "@+id/button4" android:layout_height = "wrap_content" android:layout_width = "match_parent" android:text = "进度条" ></ Button > |
11 | < Button android:id = "@+id/button6" android:layout_height = "wrap_content" android:layout_width = "match_parent" android:text = "自定义的" ></ Button > |
12 | < Button android:id = "@+id/button7" android:layout_height = "wrap_content" android:layout_width = "match_parent" android:text = "退出" ></ Button > |
自定义的Dialog风格 style.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
03 | xmlns:android = "http://schemas.android.com/apk/res/android" |
04 | android:orientation = "vertical" |
05 | android:layout_width = "match_parent" |
06 | android:layout_height = "match_parent" android:weightSum = "1" > |
07 | < ImageView android:layout_height = "wrap_content" android:src = "@drawable/logo" android:id = "@+id/imageView1" android:layout_width = "fill_parent" ></ ImageView > |
09 | android:orientation = "horizontal" |
10 | android:layout_width = "fill_parent" |
11 | android:layout_height = "wrap_content" |
13 | < TextView android:text = "账号:" android:id = "@+id/textView1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" ></ TextView > |
14 | < EditText android:layout_height = "wrap_content" android:layout_weight = "1" android:layout_width = "wrap_content" android:id = "@+id/editText1" > |
15 | < requestFocus ></ requestFocus > |
20 | android:orientation = "horizontal" |
21 | android:layout_width = "fill_parent" |
22 | android:layout_height = "wrap_content" |
24 | < TextView android:text = "密码:" android:id = "@+id/textView2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" ></ TextView > |
25 | < EditText android:layout_height = "wrap_content" android:layout_weight = "1" android:layout_width = "wrap_content" android:id = "@+id/editText2" android:inputType = "textPassword" ></ EditText > |
主页面
转载于:https://my.oschina.net/pangzhuzhu/blog/318131