本文主要是介绍自定义Dialog---继承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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!