您的位置:首页 > 移动开发 > Android开发

Android弹出自定义对话框

2012-08-07 11:32 302 查看
1.建立需要弹出的对话框的布局

布局中一共有三个控件,分别为:CheckBox、RatingBar、Button。布局文件为dialog.xml,具体代码如下:

<?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" >

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />

<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>


效果图:



2.弹出对话框
弹出对话框的主要代码如下:

private void showDialog(){
view=this.getLayoutInflater().inflate(R.layout.dialog, null);
dialog=new Dialog(this);
dialog.setTitle("测试");
dialog.setContentView(view);

btn=(Button)view.findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});

dialog.show();
}


效果图:



来源: http://www.lrguan.com/articles/20130709-01.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: