美文网首页
智能终端软件开发——自定义对话框

智能终端软件开发——自定义对话框

作者: itczt | 来源:发表于2019-03-22 10:27 被阅读0次

自定义对话框

在Android项目中为了提高用户体验,达到更理想的效果,一般不使用系统提供的对话框,而是根据项目需求定义自己的对话框的样式。Dialog对话框就是经常需要自己定义的,接下来就是通过代码来演示自定义Dialog的步骤。

1.创建布局

创建一个自定义对话框的布局文件,指定名称为my_dialog,布局中需要设定对话框的标题、对话框的内容以及“确定”与“取消”按钮。

activity_two.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#ffffff"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="#0080ff"
            android:gravity="center"
            android:text="自定义对话框"
            android:textColor="#ffffff"
            android:textSize="18sp"
            android:visibility="visible"/>

        <LinearLayout
            android:id="@+id/ll_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">

            <TextView
                android:id="@+id/td_msg"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:minHeight="100dp"
                android:paddingBottom="15dp"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingTop="15dp"
                android:textSize="16sp"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_gravity="bottom"
            android:background="#e0e0e0"
            android:gravity="center"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn_ok"
                android:layout_width="114dp"
                android:layout_height="40dp"
                android:layout_marginLeft="20dp"
                android:background="#ff8000"
                android:gravity="center"
                android:text="确定"
                android:textColor="#ffffff"
                android:textSize="15sp"/>
            <Button
                android:id="@+id/btn_cancel"
                android:layout_width="114dp"
                android:layout_height="40dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:background="#d0d0d0"
                android:gravity="center"
                android:text="取消"
                android:textColor="#666666"
                android:textSize="15sp"/>
        </LinearLayout>
    </LinearLayout>
</FrameLayout>

2.创建自定义对话框

创建一个MyDialog类继承自Diaing类,主要用于初始化自定义对话框中的控件以及相应按钮的点击事件。

MyDialog.java

package com.czt.twoapp;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class MyDialog extends Dialog {
    private String dialogName;
    private TextView tvMsg;
    private Button btnOK;
    private Button btnCancel;
    public MyDialog(Context context,String dialogName){
        super(context);
        this.dialogName = dialogName;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题
        setContentView(R.layout.activity_two);//引入自定义对话框布局
        tvMsg = (TextView)findViewById(R.id.td_msg);
        btnOK = (Button)findViewById(R.id.btn_ok);
        btnCancel = (Button)findViewById(R.id.btn_cancel);
        tvMsg.setText(dialogName);//设置自定义对话框内容
        //为“确定”按钮设置点击事件
        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击“确定”按钮时的操作
            }
        });
            //为“取消”按钮设置点击事件
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();//关闭当前对话框
            }
        });
    }
}

从上述代码可以看出,在自定义对话框的onCreate()方法中,通过setContentView()方法就可以把自定义对话框的布局显示出来,并且可以按照需求来操作自定义对话布局的控件。

3.使用自定义对话框

在MainActivity中,只要调用MyDialog的构造方法就可以把自定义的对话框显示出来。

twoActivity.java

package com.czt.twoapp;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class twoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        MyDialog myDialog = new MyDialog(this,"我是自定义的Dialog");
        myDialog.show();
    }
}    

接下来运行程序


运行结果

可以看出,自定义对话框的界面效果可以自行设置,并且与系统自带的对话框相比更加灵活,界面更加美观。

相关文章

网友评论

      本文标题:智能终端软件开发——自定义对话框

      本文链接:https://www.haomeiwen.com/subject/npyfvqtx.html