上图
首先自定义一个CustomDialog类,继承自Dialog
package com.example.customdialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class CustomDialog extends Dialog {
private Context context;
// 布局加载器
private LayoutInflater inflater;
// 整个布局
private View parent;
// 按钮
private Button bt;
// 输入框
private EditText ed;
// 标题
private TextView title;
public CustomDialog(Context context) {
super(context);
this.context = context;
this.inflater = LayoutInflater.from(context);
// 设置无标题
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// 加载布局
parent = inflater.inflate(R.layout.layout_custom_dialog, null);
// 设置布局
setContentView(parent);
// 不可取消
setCancelable(false);
}
public Button getBt() {
if (bt == null) {
bt = (Button) parent.findViewById(R.id.dialog_bt);
}
return bt;
}
public EditText getEd() {
if (ed == null) {
ed = (EditText) parent.findViewById(R.id.dialog_ed);
}
return ed;
}
public TextView getTitle() {
if (title == null) {
title = (TextView) parent.findViewById(R.id.dialog_title);
}
return title;
}
}
自定义的dialog要加载一个布局文件,所以写一个xml文件作为dialog的布局,这个布局可以自己定义,搞成自己喜欢的样式,添加自己需要的控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/dialog_title"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="please input"
android:textSize="20sp" />
<EditText
android:id="@+id/dialog_ed"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:hint="input" />
<Button
android:id="@+id/dialog_bt"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="ok" />
</LinearLayout>
在MainActivity中使用试试看
package com.example.customdialog;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private CustomDialog customDialog;
private Button showDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showDialog = (Button) findViewById(R.id.show_dialog);
showDialog.setOnClickListener(this);
customDialog = new CustomDialog(MainActivity.this);
customDialog.getBt().setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.show_dialog:
customDialog.show();
break;
case R.id.dialog_bt:
if (customDialog.getEd().getText().toString().isEmpty()) {
toast("please input!");
customDialog.getEd().requestFocus();
} else {
toast("you input " + customDialog.getEd().getText().toString());
customDialog.dismiss();
}
default:
break;
}
}
private void toast(String string) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT)
.show();
}
}
附上main布局
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:id="@+id/show_dialog" />
</RelativeLayout>
------granton------