今天无意中又想起了Android创建的对话框,总结了一下,使用了三种方式来创建
第一种,使用Android自带的Theme.Dialog主题;
第二种,使用Builder和AlertDialog来创建;
第三种,使用继承自PopupWindow来创建;
直接上代码:
先看MainActivity的:
package com.example.zhchuanjianduihuakuang;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
private Button btn1,btn2,btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)this.findViewById(R.id.btn1);
btn2=(Button)this.findViewById(R.id.btn2);
btn3=(Button)this.findViewById(R.id.btn3);
btn1.setOnClickListener(new Click());
btn2.setOnClickListener(new Click());
btn3.setOnClickListener(new Click());
}
private class Click implements OnClickListener{
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn1:
FirstDialog();
break;
case R.id.btn2:
SecondDialog();
break;
case R.id.btn3:
ThirdDialog();
break;
}
}
}
//调用第一个对话框
private void FirstDialog(){
Intent intent=new Intent(MainActivity.this,DialogActivity.class);
startActivity(intent);
}
//调用第二个对话框
private void SecondDialog(){
SecondDialog seconddialog=new SecondDialog(MainActivity.this);
seconddialog.setDialog();
}
//调用第三个对话框
private void ThirdDialog(){
ThirdDialog thirddialog=new ThirdDialog(MainActivity.this);
thirddialog.setDialog();
//设置该PopupWindow的显示的位置,这个设置是距离底部98
thirddialog.showAtLocation(MainActivity.this.findViewById(R.id.main_layout), Gravity.BOTTOM, 0, 98);
}
}
对于第一种方式,我们先创建一个Activity,我在这命名为DialogActivity,在AndroidManifest中设置它的theme为dialog,代码如下:
<activity
android:name="com.example.zhchuanjianduihuakuang.DialogActivity"
android:theme="@android:style/Theme.Dialog"
android:label="@string/title_activity_dialog" >
</activity>
第二种方式的代码:
package com.example.zhchuanjianduihuakuang;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
public class SecondDialog {
private Context context;
private Builder builder;
private AlertDialog dialog;
private Button btnquxiao;
public SecondDialog(Context context){
this.context=context;
}
public void setDialog(){
View dialogview=LayoutInflater.from(context).inflate(R.layout.seconddialog, null);
btnquxiao=(Button)dialogview.findViewById(R.id.btnquxiao2);
btnquxiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.cancel();
}
});
builder=new AlertDialog.Builder(context);
builder.setMessage("这是第二个对话框");
builder.setView(dialogview);
dialog=builder.create();
dialog.show();
}
}
第三种方式的代码:
package com.example.zhchuanjianduihuakuang;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
public class ThirdDialog extends PopupWindow {
private Context context;
private Button btnquxiao;
public ThirdDialog(Context context){
this.context=context;
}
public void setDialog(){
View dialogview=LayoutInflater.from(context).inflate(R.layout.thirddialog, null);
btnquxiao=(Button)dialogview.findViewById(R.id.btnquxiao3);
btnquxiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
dialogview.setVisibility(View.VISIBLE);
this.setContentView(dialogview);//设置这个popupwindow要显示的view
this.setWidth(LayoutParams.WRAP_CONTENT);//设置它的宽度
this.setHeight(LayoutParams.WRAP_CONTENT);//设置它的高度
/**
还有许多的设置,例如设置进入的动画
this.setAnimationStyle()
设置drawable
this.setBackgroundDrawable()
等等
*/
}
}
它们的布局也很简单,你也可以共用一个布局,也可以跟我这样分开来写:
第一个布局:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".DialogActivity" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是第一个对话框"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnquxiao"
android:text="取消"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
第二个布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnquxiao2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"/>
</LinearLayout>
第三个布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:text="第三种创建对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnquxiao3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"/>
</LinearLayout>
这样,这三种创建对话框的方式就完了,如果还有别的,希望告知,一起学习。
附上该demo的下载地址:
http://download.csdn.net/detail/zhouconglong/8507881