项目汇总之常见的对话框
程序员文章站
2022-05-16 14:35:21
...
项目中,经常会有弹出对话框,之前的文章有提到懒人dialog,不过那是第三方的,而现在这几个完全是自定义的,先看看效果:
代码:
package com.gyq.utilstest;
import android.app.AlertDialog;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.gyq.utilstest.base.ActivityFragmentInject;
import com.gyq.utilstest.base.BaseActivity;
import com.gyq.utilstest.utils.ToastUtil;
import com.gyq.utilstest.widget.ContainsEmojiEditText;
@ActivityFragmentInject(
contentViewId = R.layout.activity_main,
hasNavigationView = false,
hasToolbar = true,
toolbarTitle = R.string.lab_bind,
toolbarLeftIcon = R.drawable.selector_back
)
public class MainActivity extends BaseActivity {
private EditText mText;
private Button mOk;
private TextView mContent;
// private
@Override
protected void findViewAfterViewCreate() {
mText = (EditText)findViewById(R.id.edt_text);
mOk = (Button)findViewById(R.id.btn_ok);
mContent = (TextView)findViewById(R.id.tv_content);
}
@Override
protected void initDataAfterFindView() {
mOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*String text = mText.getText().toString();
MLog.d("ZM",text);
if (!EditTextUtil.isAvailString(text,14)) {
MLog.d("ZM",text + ">>>>>>14");
ToastUtil.showToast(getString(R.string.device_name_too_long));
return;
}
mContent.setText(text);*/
//中间对话框
showDialog();
//showVersionDialog();
//updateDoorDialog();
}
});
// mPayInput.onClick(mContent);
}
@Override
public void leftIconClick(View view) {
}
private AlertDialog dialog;
private void showDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
dialog = builder.create();
View view = View.inflate(this,R.layout.dialog_delete_edit,null);
TextView txtUse = (TextView) view.findViewById(R.id.txt_use);
TextView txtEdit = (TextView) view.findViewById(R.id.txt_edit);
TextView txtDelete = (TextView) view.findViewById(R.id.txt_delete);
txtUse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
txtEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
txtDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.setView(view);
dialog.show();
}
@Override
public void rightIconClick(View view) {
finish();
}
private AlertDialog versionDialog;
private void showVersionDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
versionDialog = builder.create();
View view = View.inflate(this, R.layout.dialog_device_add_notice, null);
final LinearLayout llBefore = (LinearLayout)view.findViewById(R.id.ll_before);
final LinearLayout llAfter = (LinearLayout)view.findViewById(R.id.ll_after);
final ImageView imgWaiting = (ImageView) view.findViewById(R.id.img_config_icon1);
//final TextView txtMsg = (TextView) view.findViewById(R.id.txt_msg);
TextView txtTitle = (TextView) view.findViewById(R.id.txt_title);
txtTitle.setText(getString(R.string.base_update_notice));
final TextView txtMsg = (TextView) view.findViewById(R.id.txt_msg);
TextView txtCancle = (TextView) view.findViewById(R.id.txt_cancle);
txtMsg.setText(getString(R.string.base_need_update));
txtMsg.setGravity(Gravity.CENTER);
txtCancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
versionDialog.dismiss();
}
});
TextView txtOk = (TextView) view.findViewById(R.id.txt_ok);
llBefore.setVisibility(View.VISIBLE);
llAfter.setVisibility(View.GONE);
txtOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//versionDialog.dismiss();
//updateVersion();
txtMsg.setText("正在搜索,请稍后...");
llBefore.setVisibility(View.GONE);
llAfter.setVisibility(View.VISIBLE);
AnimationDrawable animationDrawable = (AnimationDrawable) imgWaiting.getDrawable();
if(animationDrawable != null){
animationDrawable.start();
}
}
});
versionDialog.setView(view);
versionDialog.show();
}
private AlertDialog monNameDialog;
private void updateDoorDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
monNameDialog = builder.create();
View view = View.inflate(this, R.layout.dialog_mon_name, null);
final ContainsEmojiEditText dgName = (ContainsEmojiEditText) view.findViewById(R.id.ed_name);
view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newName = dgName.getText().toString().trim();
if (TextUtils.isEmpty(newName)) {
ToastUtil.showToast("名称不能为空!");
return;
}
if (monNameDialog != null) monNameDialog.dismiss();
}
});
view.findViewById(R.id.btn_cancle).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (monNameDialog != null) monNameDialog.dismiss();
}
});
monNameDialog.setView(view);
monNameDialog.show();
}
}
布局文件:
1.dialog_mon_name.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/DIMEN_300dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txt_title"
android:layout_width="match_parent"
android:layout_height="@dimen/DIMEN_40dp"
android:layout_marginTop="@dimen/DIMEN_20dp"
android:gravity="center"
android:text="修改名称"
android:textColor="@color/black_ff"
android:textSize="@dimen/DIMEN_18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/DIMEN_30dp"
android:layout_marginRight="@dimen/DIMEN_30dp"
android:layout_marginTop="@dimen/DIMEN_30dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/v2_name_icon" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<com.gyq.utilstest.widget.ContainsEmojiEditText
android:id="@+id/ed_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/DIMEN_5dp"
android:background="@null"
android:paddingBottom="@dimen/DIMEN_5dp" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/DIMEN_1dp"
android:background="#e6e6e6" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/DIMEN_10dp"
android:layout_marginLeft="@dimen/DIMEN_30dp"
android:layout_marginRight="@dimen/DIMEN_30dp"
android:layout_marginTop="@dimen/DIMEN_40dp">
<Button
android:id="@+id/btn_cancle"
android:layout_width="0dp"
android:layout_height="@dimen/DIMEN_40dp"
android:layout_marginBottom="@dimen/DIMEN_10dp"
android:layout_marginRight="@dimen/DIMEN_20dp"
android:layout_weight="1"
android:background="@drawable/btn_code_rq_nor"
android:text="取消"
android:textSize="@dimen/DIMEN_15sp" />
<Button
android:id="@+id/btn_confirm"
android:layout_width="0dp"
android:layout_height="@dimen/DIMEN_40dp"
android:layout_marginBottom="@dimen/DIMEN_10dp"
android:layout_marginLeft="@dimen/DIMEN_20dp"
android:layout_weight="1"
android:background="@drawable/v2_btn_blue_ok"
android:text="确定"
android:textColor="@color/white_ff"
android:textSize="@dimen/DIMEN_15sp" />
</LinearLayout>
</LinearLayout>
2.dialog_device_add_notice.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_before"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txt_title"
android:layout_width="match_parent"
android:layout_height="@dimen/DIMEN_40dp"
android:layout_marginTop="@dimen/DIMEN_20dp"
android:gravity="center"
android:text="操作提示"
android:textColor="@color/black_ff"
android:textSize="@dimen/DIMEN_18sp" />
<TextView
android:id="@+id/txt_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/DIMEN_20dp"
android:layout_marginLeft="@dimen/DIMEN_40dp"
android:layout_marginRight="@dimen/DIMEN_40dp"
android:layout_marginTop="@dimen/DIMEN_20dp"
android:text=""
android:textColor="@color/black_aa"
android:textSize="@dimen/DIMEN_15sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/DIMEN_20dp">
<TextView
android:id="@+id/txt_cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/btn_code_rq_nor"
android:gravity="center"
android:paddingBottom="@dimen/DIMEN_8dp"
android:paddingLeft="@dimen/DIMEN_30dp"
android:paddingRight="@dimen/DIMEN_30dp"
android:paddingTop="@dimen/DIMEN_8dp"
android:text="取消"
android:textSize="@dimen/DIMEN_15sp" />
<TextView
android:id="@+id/txt_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/v2_btn_blue_ok"
android:gravity="center"
android:paddingBottom="@dimen/DIMEN_8dp"
android:paddingLeft="@dimen/DIMEN_30dp"
android:paddingRight="@dimen/DIMEN_30dp"
android:paddingTop="@dimen/DIMEN_8dp"
android:text="确定"
android:textColor="@color/white_ff"
android:textSize="@dimen/DIMEN_15sp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_after"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<ImageView
android:id="@+id/img_config_icon1"
android:layout_width="@dimen/DIMEN_50dp"
android:layout_height="@dimen/DIMEN_50dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/DIMEN_30dp"
android:src="@drawable/v2_ani_gateway_adding" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/DIMEN_30dp"
android:layout_marginTop="@dimen/DIMEN_20dp"
android:gravity="center"
android:text="连接中,请稍后..."
android:textColor="@color/base_normal_txt_title"
android:textSize="@dimen/DIMEN_15sp" />
</LinearLayout>
</RelativeLayout>
3.dialog_delete_edit.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_height="match_parent"
android:background="@color/white_ff"
android:orientation="vertical">
<TextView
android:id="@+id/txt_use"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/DIMEN_15dp"
android:text="使用场景"
android:textColor="@color/base_normal_txt_title"
android:textSize="@dimen/DIMEN_15sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/bg_detail"></View>
<TextView
android:id="@+id/txt_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/DIMEN_15dp"
android:text="编辑场景"
android:textColor="@color/base_normal_txt_title"
android:textSize="@dimen/DIMEN_15sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/bg_detail"></View>
<TextView
android:id="@+id/txt_delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/DIMEN_15dp"
android:text="删除场景"
android:textColor="@color/base_normal_txt_title"
android:textSize="@dimen/DIMEN_15sp" />
</LinearLayout>
还有一些别的代码,会上传相应的demo。