Android-控件架构-Dialog
程序员文章站
2024-03-23 22:58:46
...
public class VersionUpdateDialog extends Dialog {
public VersionUpdateDialog(@NonNull Context context) {
super(context);
}
public VersionUpdateDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
public static class Builder{
private Context context;
private String title;
private String message;
private String positiveButtonText;
private String negativeButtonText;
private View contentView;
private DialogInterface.OnClickListener positiveButtonClickListener;
private DialogInterface.OnClickListener negativeButtonClickListener;
public Builder(Context context){
this.context = context;
}
/**
* 设置消息内容
* @param message
* @return
*/
public Builder setMessage(String message){
this.message = message;
return this;
}
/**
* 通过资源文件设置消息内容
* @param message
* @return
*/
public Builder setMessage(int message){
this.message = (String)context.getText(message);
return this;
}
/**
* 设置标题
* @param title
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* 通过资源文件设置标题
* @param title
* @return
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener){
this.positiveButtonText = (String) context
.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}
public Builder setPositiveButton(String positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
public Builder setNegativeButton(int negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = (String) context
.getText(negativeButtonText);
this.negativeButtonClickListener = listener;
return this;
}
public Builder setNegativeButton(String negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}
public VersionUpdateDialog create(){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final VersionUpdateDialog dialog = new VersionUpdateDialog(context, R.style.VersionUpdateDialog);
View layout = inflater.inflate(R.layout.dialog_version_update,null);
dialog.addContentView(layout,new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
layout.findViewById(R.id.to_store).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("your url");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
}
});
((TextView) layout.findViewById(R.id.title)).setText(title);
if(positiveButtonText != null){
((TextView) layout.findViewById(R.id.positiveTextView))
.setText(positiveButtonText);
if(positiveButtonClickListener != null){
((TextView) layout.findViewById(R.id.positiveTextView))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
}else {
layout.findViewById(R.id.positiveTextView).setVisibility(
View.GONE);
}
if(negativeButtonText != null){
((TextView) layout.findViewById(R.id.negativeTextView))
.setText(negativeButtonText);
if(negativeButtonClickListener != null){
((TextView) layout.findViewById(R.id.negativeTextView))
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativeButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_NEGATIVE);
}
});
}
}else{
layout.findViewById(R.id.negativeTextView).setVisibility(
View.GONE);
dialog.setCancelable(false);
}
if(message != null){
((TextView) layout.findViewById(R.id.message)).setText(message);
((TextView) layout.findViewById(R.id.message)).setMovementMethod(ScrollingMovementMethod.getInstance());
} else if(contentView != null){
((LinearLayout) layout.findViewById(R.id.content))
.removeAllViews();
((LinearLayout) layout.findViewById(R.id.content))
.addView(contentView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
}
dialog.setContentView(layout);
return dialog;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/dp_280"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="@dimen/dp_280"
android:layout_height="@dimen/dp_157"
android:scaleType="fitXY"
android:src="@mipmap/bg_dialog_version_update"/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="@dimen/sp_14"
android:layout_marginLeft="@dimen/dp_14"
android:layout_marginTop="@dimen/dp_85"
android:textColor="#4088FF"/>
</FrameLayout>
<TextView
android:id="@+id/message"
android:layout_width="@dimen/dp_280"
android:layout_height="wrap_content"
android:maxLines="5"
android:text=""
android:textSize="@dimen/sp_14"
android:paddingVertical="@dimen/dp_15"
android:paddingHorizontal="@dimen/dp_20"
android:textColor="@color/wallet_text_color666666"
android:lineSpacingExtra="@dimen/dp_10"
android:scrollbars="vertical"
android:background="@color/white">
</TextView>
<LinearLayout
android:layout_width="@dimen/dp_280"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="@drawable/shape_versionupdate_buttom"
android:paddingVertical="@dimen/dp_22"
android:orientation="vertical">
<TextView
android:id="@+id/positiveTextView"
android:layout_width="@dimen/dp_225"
android:layout_height="@dimen/dp_44"
android:text=""
android:gravity="center"
android:textStyle="bold"
android:clickable="true"
android:textSize="@dimen/sp_16"
android:textColor="@color/white"
android:background="@drawable/shape_versionupdate_button"/>
<TextView
android:id="@+id/negativeTextView"
android:layout_width="@dimen/dp_225"
android:layout_height="@dimen/dp_44"
android:text=""
android:clickable="true"
android:gravity="center"
android:textSize="@dimen/sp_14"
android:textColor="@color/wallet_text_color666666"/>
<TextView
android:id="@+id/to_store"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#2BA4FF"
android:gravity="center"
android:paddingVertical="@dimen/dp_5"
android:layout_marginTop="@dimen/dp_10"
android:text="跳转应用宝"/>
</LinearLayout>
</LinearLayout>
drawable只放一个例子
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="@dimen/dp_8" />
<solid android:color="#2BA4FF" />
<stroke android:width="1dp" android:color="#2BA4FF" />
</shape>
上一篇: Saving Tang Monk -HDU - 5025(bfs状态压缩)
下一篇: Raspberry Pi 3 Model B 无显示器安装Raspbian系统(基于Debian Stretch)
推荐阅读