安卓开发如何自定义Dialog

安卓开发如何自定义Dialog

       我没怎么学安卓开发,因为工作上的项目需要,边学边做;在做项目的过程中真切的体会到了接口回调在安卓开发中使用的频率与场景非常之多。(个人感觉使用接口回调主要是突破安卓变量的作用域范围的限制一种解决方案。安卓开发中也使用到了很多设计模式。)
       差点离题了,下面将详细解释自定义Dialog的使用。

自定义Dialog

       自定义Dialog主要包含两部分,一个是dialog的布局文件,另外一个就是Dialog的构造类,下面直接上代码了,布局文件如下:

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

    <TextView
        android:id="@+id/update_dialog_title"
        android:layout_width="280dp"
        android:layout_height="wrap_content"
        android:text="标题"
        android:textSize="25dp"
        android:background="@color/colorBlue"
        android:textColor="@color/colorBlack"
        android:gravity="center"/>

    <LinearLayout
        android:layout_width="280dp"
        android:layout_height="65dp"
        android:layout_marginTop="15dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="结果:"
            android:textSize="25dp"
            android:gravity="center_horizontal"
            />

        <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/spinnerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/spinner"
            android:gravity="center_horizontal"
            android:ems="10"
            android:inputType="textPersonName"
            android:layout_weight="1"
            android:text=""
            android:textSize="25dp">

        </androidx.appcompat.widget.AppCompatSpinner>

    </LinearLayout>

    <LinearLayout
        android:layout_width="280dp"
        android:layout_height="60dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView9"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:layout_weight="2"
            android:text="备注:"
            android:textSize="25dp"/>

        <EditText
            android:id="@+id/update_dialog_remarks"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:ems="10"
            android:inputType="textPersonName"
            android:layout_weight="1"
            android:text=""
            android:textSize="25dp"/>


    </LinearLayout>


    <LinearLayout
        android:layout_width="280dp"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:gravity="center">

        <Button
            android:id="@+id/positiveBtn"
            android:layout_width="80dp"
            android:layout_height="45dp"
            android:layout_marginRight="15dp"
            android:background="@color/colorBlue"
            android:text="修改" />

        <Button
            android:id="@+id/negtiveBtn"
            android:layout_width="80dp"
            android:layout_height="45dp"
            android:layout_marginLeft="15dp"
            android:background="@color/colorBlue"
            android:text="取消" />

    </LinearLayout>

</LinearLayout>

自定义的Dialog类如下:

package cn.com.vivo.soft.lm.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyboardShortcutGroup;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

import butterknife.BindView;
import cn.com.vivo.soft.lm.R;
import cn.com.vivo.soft.lm.domain.item.SysTestPlanItemResult;
import cn.com.vivo.soft.lm.widget.activitys.TestItemResultInputActivity;

public class UpdateItemResultDialog extends Dialog {
    public interface OnClickBottomListener{
        /**
         * 点击确定按钮事件
         */
        void onPositiveClick();

        /**
         * 点击取消按钮事件
         */
        void onNegtiveClick();
    }
    private List<SysTestPlanItemResult> results;
    private int position;
    private String title;
    private String resultSpStr;
    public OnClickBottomListener onClickBottomListener;
    Context mContext;

    TextView update_dialog_title;

    Spinner resultSp;

    EditText update_dialog_remarks;

    Button negtiveBtn;

    Button positiveBtn;

    public UpdateItemResultDialog(@NonNull Context context,List<SysTestPlanItemResult> results,int position,String title) {
        super(context);
        this.mContext=context;
        this.results=results;
        this.position=position;
        this.title=title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_item_result_dialog);
        //按空白处不能取消动画,API已提供
        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        //初始化界面数据
        refreshView();
        //初始化界面控件的事件
        initEvent();
    }

    private void  initView(){
        update_dialog_title=findViewById(R.id.update_dialog_title);
        resultSp=findViewById(R.id.spinnerView);
        update_dialog_remarks=findViewById(R.id.update_dialog_remarks);
        negtiveBtn=findViewById(R.id.negtiveBtn);
        positiveBtn=findViewById(R.id.positiveBtn);
    }

    private void refreshView(){
        update_dialog_title.setText(this.title);
//        resultSp.setTooltipText();
        if(results.get(position).getResult()!=null){
            if(results.get(position).getResult().equals("异常")){
                resultSp.setSelection(2);
            }else {//正常
                resultSp.setSelection(1);
            }
        }else {//空
            resultSp.setSelection(0);
        }

        //备注信息
        update_dialog_remarks.setText(results.get(position).getRemarks());
    }

    /**
     * 设置确定与取消按钮事件
     */
    private void initEvent(){
        //设置确定按钮被点击后,向外界提供监听
        positiveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onClickBottomListener!=null){
                    onClickBottomListener.onPositiveClick();
                }
            }
        });

        //设置取消按钮被点击后,向外界提供监听
        negtiveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onClickBottomListener!=null){
                    onClickBottomListener.onNegtiveClick();
                }
            }
        });

        resultSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                resultSpStr= mContext.getResources().getStringArray(R.array.spinner)[i];
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    public UpdateItemResultDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener){
        this.onClickBottomListener=onClickBottomListener;
        return this;
    }

    public Spinner getResultSp() {
        return resultSp;
    }

    public void setResultSp(Spinner resultSp) {
        this.resultSp = resultSp;
    }

    public EditText getUpdate_dialog_remarks() {
        return update_dialog_remarks;
    }

    public void setUpdate_dialog_remarks(EditText update_dialog_remarks) {
        this.update_dialog_remarks = update_dialog_remarks;
    }

    public String getResultSpStr() {
        return resultSpStr;
    }

    public void setResultSpStr(String resultSpStr) {
        this.resultSpStr = resultSpStr;
    }
}

       自定义Dialog首先要继承Dialog,覆写它的onCreate()方法,OnCreate是初始化方法,在里面我们要加载布局文件将界面控件、界面数据、界面控件事件设置好。自定义Dialog的构造方法是很重要的,Dialog界面显示的数据就是通过构造方法传输过来的,如上面的results、title、position便是传递过来的数据。拿到数据之后便可以将数据渲染到界面上。
       自定义Dialog最重要的点是实现监听事件。一般在Dialog内部或外部定义一个事件接口,如上代码我是定义在Dialog内部的(OnClickBottomListener),分别写了两个方法一个确定事件一个取消事件。注意下面这段代码,我们要为Dialog设置接口,并且返回对象是Dialoh本身。

public UpdateItemResultDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener){
        this.onClickBottomListener=onClickBottomListener;
        return this;
    }

       如上我在initEvent中设置取消与确定两个事件的监听事件,在监听事件的内部调用的还是OnClickBottomListener接口的方法。当我们自定义Dialog时先实例化对象,在调用上面的setOnClickBottomListener,实例化OnClickBottomListener 对象,就可以写自己的业务逻辑。这就是一种典型的接口回调方法。
     阅读完了,点个赞呗。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值