自定义输入对话框,调用者决定对话框按钮的功能


dialog_custominput.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/tx_input_dialog_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题"
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        />
    <EditText
        android:id="@+id/et_input_dialog_message"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        >
        <Button
            android:id="@+id/bt_input_dialog_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#0000ff"
            android:text="取消"
            />
        <Button
            android:id="@+id/bt_input_dialog_confirm"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textColor="#0000ff"
            android:text="确认"
            />

    </LinearLayout>

</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.smartsms.adolpher.myapplication.MainActivity">
    


</RelativeLayout><strong>
</strong>

CustomInputDialog.class

package com.smartsms.adolpher.myapplication;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by Adolpher on 2016/10/16.
 */
public class CustomInputDialog extends AlertDialog implements View.OnClickListener{
    private String title;
    private TextView tx_input_dialog_title;
    private EditText et_input_dialog_message;
    private Button bt_input_dialog_cancel;
    private Button bt_input_dialog_confirm;
    private OnCustomInputDialogListener customInputDialogListener;
    protected CustomInputDialog(Context context, String title, OnCustomInputDialogListener customInputDialogListener) {
        super(context);
        this.title = title;
        this.customInputDialogListener = customInputDialogListener;
    }
    public static void showCustomInputDialog(Context context, String title, OnCustomInputDialogListener customInputDialogListener){
        CustomInputDialog dialog = new CustomInputDialog(context, title, customInputDialogListener);
        //对话框默认没有edittext,真机上无法出现输入键盘
        dialog.setView(new EditText(context));
        dialog.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custominput);
        tx_input_dialog_title = (TextView) findViewById(R.id.tx_input_dialog_title);
        et_input_dialog_message = (EditText) findViewById(R.id.et_input_dialog_message);
        bt_input_dialog_cancel = (Button) findViewById(R.id.bt_input_dialog_cancel);
        bt_input_dialog_confirm = (Button) findViewById(R.id.bt_input_dialog_confirm);
        bt_input_dialog_cancel.setOnClickListener(this);
        bt_input_dialog_confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_input_dialog_cancel:
                customInputDialogListener.cancel();
                break;
            case R.id.bt_input_dialog_confirm:
                customInputDialogListener.comfirm(et_input_dialog_message.getText().toString());
                break;
        }
        dismiss();
    }
    //定义接口 具体方法在调用时实现
    public interface OnCustomInputDialogListener{
        void cancel();
        //因为调用者不方便直接拿到edittext里面的值,所以直接传进去
        void comfirm(String message);
    }
}

MainActivity.class

package com.smartsms.adolpher.myapplication;

import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AutoCompleteTextView;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomInputDialog.showCustomInputDialog(this, "这是测试", new CustomInputDialog.OnCustomInputDialogListener() {
            @Override
            public void cancel() {

            }
            @Override
            public void comfirm(String message) {
                Toast.makeText(MainActivity.this, message+" 这个按钮的功能是由调用者定义", Toast.LENGTH_LONG).show();
            }
        });
    }
}<strong>
</strong>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值