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>