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

android自定义对话框

2015-10-31 21:08 453 查看
对话框是android开发中用的比较多的控件,一般都是用来提示,但是对话框的功能不局限于提示,还可以自定义对话框让用户进行选择。如下图,当点击按钮时,弹出对话框让用户选择:



那么该如何实现这样的功能呢?(以下步骤不分先后)

1、在项目res/layout文件夹下创建自定义对话框的布局文件,如item_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#cccccc"
android:layout_height="match_parent">
<TextView
style="@style/text"
android:id="@+id/tv_name"
android:text="姓名"/>
<TextView style="@style/divide"/>
<TextView
style="@style/text"
android:id="@+id/tv_address"
android:text="地址"/>
<TextView style="@style/divide"/>
<TextView
style="@style/text"
android:id="@+id/tv_birsday"
android:text="生日"/>
<TextView style="@style/divide"/>
<TextView
style="@style/text"
android:id="@+id/tv_cancel"
android:text="取消"/>
</LinearLayout>


用到的style如下:

<!--文本框样式-->
<style name="text">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:textSize">15sp</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:background">@drawable/shape_text</item>
</style>
<!---去掉对话框标题栏-->
<style name="dialogNoTitle">
<item name="android:windowNoTitle">true</item>
</style>
<!---分割线-->
<style name="divide">
<item name="android:layout_height">1dp</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginRight">10dp</item>
<item name="android:layout_marginTop">5dp</item>
<item name="android:background">#ff33ff</item>
</style>


2、在drawable目录下定义显示文本的形状shape_text.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"></corners>
<solid android:color="#99ff00"></solid>
<stroke android:width="1dp"
android:color="#ff3366"></stroke>
</shape>


3、在Activity的java代码中添加对话框的实现。

public class MainActivity extends Activity implements View.OnClickListener {
private int[] location = new int[2];         //组件的位置,用于让对话框出现在点击组件的位置
private int excursionX = 0;                  //对话框相对组件的偏移量
private int excursionY = 0;
private int dialogWidth = 0;                 //对话框宽高
private int dialogHeight = 0;
private Button btnAddMSG;                   //添加信息按钮
private TextView tvName;
private TextView tvAddress;
private TextView tvBirsday;
private TextView tvCancel;
private Dialog dialog;                      //对话框
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAddMSG = (Button)findViewById(R.id.btn_addMSG);
btnAddMSG.setOnClickListener(this);
}
/**
* 对对话框组件初始化
* **/
private void init(Dialog dialog){
tvName = (TextView)dialog.findViewById(R.id.tv_name);
tvAddress = (TextView)dialog.findViewById(R.id.tv_address);
tvBirsday = (TextView)dialog.findViewById(R.id.tv_birsday);
tvCancel = (TextView)dialog.findViewById(R.id.tv_cancel);
tvName.setOnClickListener(this);
tvAddress.setOnClickListener(this);
tvBirsday.setOnClickListener(this);
tvCancel.setOnClickListener(this);
}
public void showToast(String msg){
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
}
/***
*显示自定义对话框
* @param v     点击弹出对话框的组件,需要用来计算对话框出现的位置
* */
public void showShelfDlg(View v){
dialog = new Dialog(this,R.style.dialogNoTitle);   //实现对话框去掉标题栏
dialog.setContentView(R.layout.item_dialog);      //设置对话框的布局
init(dialog);               //初始化对话框控件
//获取组件位置存入location中
v.getLocationInWindow(location);
//设置对话框相对组件偏移量
excursionX = v.getWidth();
excursionY = v.getHeight();
setDlgLocation(dialog,location,excursionX,excursionY,dialogWidth,dialogHeight);
dialog.show();
}
/**设置对话框的位置以及大小
* @param location  点击的组件的位置
* @param excursionX 对话框相对组件的偏移量
* @param excursionY  同上
* @param dialogWidth 对话框宽高
* @param dialogHeight
* **/
public void setDlgLocation(Dialog dialog,int[] location,int excursionX,int excursionY,
int dialogWidth,int dialogHeight){
//获取窗口对象
Window dialogWin = dialog.getWindow();
//获取对话框的属性集
WindowManager.LayoutParams wlp = dialogWin.getAttributes();
/**
* 设置对话框的默认出现位置,Gravity.LEFT表示对话框出现在窗口左边,Gravity.TOP在窗口右边
* Gravity.LEFT|Gravity.TOP表示出现在窗口左上角,其他同理
* */
dialogWin.setGravity(Gravity.LEFT|Gravity.TOP);
//设置对话框坐标,从左上角算起
wlp.x = location[0]+excursionX/2;
wlp.y = location[1]-excursionY*3;
//设置对话框宽高
wlp.width = dialogWidth;
wlp.height = dialogHeight;
//wlp.alpha = 0.5f            透明度,需要的可以把注释去掉
dialogWin.setAttributes(wlp);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_addMSG:
dialogWidth = 250;
dialogHeight = 450;
showShelfDlg(v);
break;
case R.id.tv_name:
showToast("you select Name");
break;
case R.id.tv_address:
showToast("you select Address");
break;
case R.id.tv_birsday:
showToast("you select Birsday");
break;
case R.id.tv_cancel:
showToast("you select Cancel");
dialog.cancel();
break;
}
}
}


最后再来一张效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: