这段时间在做一个项目,需要使用到自定义Dialog,先在网上找了一下资料,发现还是有很多没有讲清楚的,在此给出一个Demo,一来可以方便广大码农,二来也可以方便自己,以备不时之需。。。
一个app更新对话框
java代码:
package com.xiebao.util.dialog;import android.app.Dialog;import android.content.Context;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.TextView;import com.xiebao.R;import com.xiebao.bean.VersionBean;public class UpdateDialog extends Dialog { @Override public void setOnDismissListener(OnDismissListener listener) { // TODO Auto-generated method stub super.setOnDismissListener(listener); } private View updateButton; private View cancelButton; private TextView mVersionNoText; private TextView mUpdateMessageText; private VersionBean mVersion; private Context context; public UpdateDialog(Context context, VersionBean mVersion) { this(context, android.R.style.Theme_Translucent_NoTitleBar); this.mVersion = mVersion; this.context=context; showDialog(); } private void showDialog() { if(!isShowing()){ show(); } } public UpdateDialog(Context context) { super(context); // TODO Auto-generated constructor stub } public UpdateDialog(Context context, int theme) { super(context, theme); // TODO Auto-generated constructor stub } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.update_version_view); initView(); initListener(); } private void initListener() { mVersionNoText.setText("版本 : " +mVersion.getNo()); mUpdateMessageText.setText(mVersion.getDescription()); updateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //要处理的业务 link(); canDialog(); } }); cancelButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { canDialog(); } }); } /** * 跳转浏览器 */ private void link() { // TODO Auto-generated method stub String url = mVersion.getUrl(); // web address Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); context.startActivity(intent); } protected void canDialog() { if(isShowing()){ dismiss(); } } private void initView() { updateButton = findViewById(R.id.update_dialog_ok); cancelButton = findViewById(R.id.update_dialog_cancel); mVersionNoText = (TextView) findViewById(R.id.update_version); mUpdateMessageText = (TextView) findViewById(R.id.update_info); }}
布局文件
R.layout.update_version_view1 26 7 15 16 10320 21 40 4130 31 39 45 46 50 51 61 6260 67 68 72 73 10283 84 90 91 101
效果图:
效果图: