码迷,mamicode.com
首页 > 其他好文 > 详细

自定义Dialog---继承Dialog

时间:2015-04-27 13:23:15      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:dialog

1.UpdateDialog

/**

 * 版本更新的dialog
 * @author lqm
 *
 */
public class UpdateDialog extends Dialog implements android.view.View.OnClickListener{


private Context context;
private Button btUpload;
private String msg;
private String btText;
private BtnClickListener btnClickListener;
private TextView tvMs;
private TextView tvMsg;

public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getBtText() {
return btText;
}
public void setBtText(String btText) {
this.btText = btText;
}

public UpdateDialog(Context context, int theme) {
super(context, theme);
this.context=context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_update);
tvMsg= (TextView) findViewById(R.id.dialogMsg);
btUpload= (Button) findViewById(R.id.btUpLoad);
tvMsg.setText(getMsg());
btUpload.setText(getBtText());
btUpload.setOnClickListener(this);

}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btUpLoad:
btnClickListener.onClick();
break;


default:
this.dismiss();
break;
}

}
public void setOnBtnclick(BtnClickListener btnClickListener) {
this.btnClickListener=btnClickListener;


}
public interface BtnClickListener{
public void onClick();
}

}


2.dialog_update.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_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:layout_height="150dp"
    android:orientation="vertical" 
    android:layout_gravity="center_horizontal"
    android:background="@color/lightblue"
    android:layout_marginBottom="@dimen/h35">
<TextView
        android:id="@+id/dialogMsg" 
        android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:text=""
    android:gravity="center"
    android:textSize="18sp"
    android:textColor="@color/white"/>


<Button
   android:id="@+id/btUpLoad"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:textColor="@color/white"
   android:textSize="18sp" 
   android:layout_gravity="center"
   android:background="@drawable/bt_blue_selector"
   android:paddingLeft="15dp"
   android:paddingRight="15dp"
   android:layout_marginBottom="16dp"
   android:paddingTop="5dp"
   android:paddingBottom="5dp"
   />


</LinearLayout>


3.使用

int serVerCode=2;
try {
int verCode=ac.getPackageManager().getPackageInfo(ac.getPackageName(), 0).versionCode;
final UpdateDialog updateDialog=new UpdateDialog(ac, R.style.dialog_style);
Window mwWindow=updateDialog.getWindow();
WindowManager.LayoutParams lp=mwWindow.getAttributes();
mwWindow.setGravity(Gravity.BOTTOM);
mwWindow.setAttributes(lp);
if(serVerCode>verCode){
updateDialog.setMsg(getResources().getString(R.string.update_mes_new));
updateDialog.setBtText(getResources().getString(R.string.update_now));
updateDialog.setOnBtnclick(new BtnClickListener() {


@Override
public void onClick() {
ToastUtils.show(ac, "正在更新。。。");
updateDialog.dismiss();
}
});


}else{
String vername=ac.getPackageManager().getPackageInfo(ac.getPackageName(), 0).versionName;
updateDialog.setMsg(getString(R.string.update_msg_now_is_new)+vername);
updateDialog.setBtText(getString(R.string.update_sure));
updateDialog.setOnBtnclick(new BtnClickListener() {


@Override
public void onClick() {
updateDialog.dismiss();
}
});
}
updateDialog.show();
} catch (NameNotFoundException e) {
e.printStackTrace();
}


自定义Dialog---继承Dialog

标签:dialog

原文地址:http://blog.csdn.net/u013042707/article/details/45307731

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!