参考:https://blog.csdn.net/sinat_31057219/article/details/77677302
自定义dialog都有自己的主题和动画效果,比如说登录提示框里面有个进度条,提示框的背影透明,dialog没有标题等,这些都需要在style中定义。
Dialog的风格主题有:
--自定义dialog背景全透明无边框theme -->
Dialog 的背景样式:
可以自己设置颜色,角度等
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<corners android:radius="6dp" />
shape>
下面是智能管家登录提示的效果实现:
duration:持续时间长短,这里是200
fromXDelta: 左方向
fromYDelta: 上方向
toXDelta: 右方向
toYDelta: 下方向
Dialog最终显示实现:
LoginActivity添加Dialog:
定义:
初始化并添加效果:
dialog显示和消失:
具体实现位置:
public class CustomDialog extends Dialog{
//自定义dialog
public CustomDialog(Context context,int layout,int style) {
this(context, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,layout,style, Gravity.CENTER);
}
public CustomDialog(Context context,int width,int height,int layout,int style,int gravity,int anim){
super(context,style);
//设置leyour
setContentView(layout);
//获取window
Window window=getWindow();
//获取window属性
WindowManager.LayoutParams layoutParams=window.getAttributes();
//修改属性
layoutParams.width=width;
layoutParams.height=height;
layoutParams.gravity=gravity;
//将属性重新设置
window.setAttributes(layoutParams);
//设置动画
window.setWindowAnimations(anim);
}
//实例
public CustomDialog(Context context,int width,int height,int layout,int style,int gravity){
this(context,width,height,layout,style,gravity,R.style.pop_anim_style);
}
}
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_name;
private EditText et_pass;
private Button btn_login;
private Button btn_registerd;
private CheckBox keep_pass;
private TextView fortget_pass;
private CustomDialog mCustomDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
}
private void initView() {
et_name=(EditText)findViewById(R.id.lo_name);
et_pass=(EditText)findViewById(R.id.lo_password);
btn_login=(Button)findViewById(R.id.lo_login);
btn_login.setOnClickListener(this);
btn_registerd=(Button)findViewById(R.id.lo_registered);
btn_registerd.setOnClickListener(this);
mCustomDialog=new CustomDialog(this,100,100,R.layout.dialog_loading,R.style.dialog_theme, Gravity.CENTER,R.style.pop_anim_style);
//屏幕外点击无效
mCustomDialog.setCancelable(false);
fortget_pass=(TextView)findViewById(R.id.forget_pass);
fortget_pass.setOnClickListener(this);
keep_pass=(CheckBox)findViewById(R.id.keep_pass);
boolean isKeep=ShareUtils.getBoolean(this,"keeppass",false);//默认为false
keep_pass.setChecked(isKeep);
//true说明记住密码,则将用户名密码显示出来
if(isKeep){
String name=ShareUtils.getString(this,"username","");
String password=ShareUtils.getString(this,"password","");
et_name.setText(name);
et_pass.setText(password);
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.lo_registered:
startActivity(new Intent(this,RegisteredActivity.class));
break;
case R.id.lo_login:
String name=et_name.getText().toString().trim();
String password=et_pass.getText().toString().trim();
if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(password)){
mCustomDialog.show();
MyUSer myUSer=new MyUSer();
myUSer.setUsername(name);
myUSer.setPassword(password);
myUSer.login(new SaveListener() {
@Override
public void done(MyUSer myUSer, BmobException e) {
if(e==null){
mCustomDialog.dismiss();
Toast.makeText(LoginActivity.this, "登录成功!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}else{
Toast.makeText(LoginActivity.this, "登录失败!", Toast.LENGTH_SHORT).show();
}
}
});
}
case R.id.forget_pass:
startActivity(new Intent(this,ForgetPasswordActivity.class));
break;
}
}
//如果输入用户名密码后退出,看是否有记住密码
@Override
protected void onDestroy() {
super.onDestroy();
//保存当前是否点击的状态
ShareUtils.putBoolean(this,"keeppass",keep_pass.isChecked());
//如果选中则保存当前用户名密码
if(keep_pass.isChecked()){
ShareUtils.putString(this,"username",et_name.getText().toString().trim());
ShareUtils.putString(this,"password",et_pass.getText().toString().trim());
}else{
//存入空
// ShareUtils.putString(this,"username","");
// ShareUtils.putString(this,"password","");
//应该删除
ShareUtils.delete(this,"username");
ShareUtils.delete(this,"password");
}
}
}
pop_in
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_shortAnimTime"
android:fromXDelta="0"
android:fromYDelta="100%"
android:toXDelta="0"
android:toYDelta="0">
translate>
set>
pop_out
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_shortAnimTime"
android:fromYDelta="0"
android:fromXDelta="0"
android:toXDelta="0"
android:toYDelta="100%">
translate>
set>
progress
?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" >
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="8"
android:useLevel="false" >
<gradient
android:centerColor="#ffffff"
android:centerY="0.50"
android:endColor="#1E90FF"
android:startColor="#ffffff"
android:type="sweep"
android:useLevel="false" />
shape>
rotate>
dialog_loading:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/dialog_white_bg"
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:indeterminateDrawable="@drawable/progress_small"
android:indeterminate="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="登录中"/>
LinearLayout>
LinearLayout>