xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:onClick="brn1Onclick"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:onClick="brn2Onclick"/>
package com.example.test13;
import android.support.v7.app.ActionBarActivity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Dialog customDialog;
private EditText zh;
private EditText pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createCustom();
}
public void createCustom(){
LayoutInflater inflater=getLayoutInflater();
View customView=inflater.inflate(R.layout.custon_view, null);
Button btnOk=(Button)customView.findViewById(R.id.et_ok);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
customDialog.dismiss();
}
});
customDialog=new Dialog(this);
customDialog.setTitle("自定义");
customDialog.setContentView(customView);
}
public void brn2Onclick(View view){
customDialog.show();
}
public void brn1Onclick(View view){
LayoutInflater inflater=getLayoutInflater();
View custonView=inflater.inflate(R.layout.custon_view, null);
zh=(EditText) custonView.findViewById(R.id.et_zh);
pwd=(EditText) custonView.findViewById(R.id.et_pwd);
new AlertDialog.Builder(this)
.setTitle("自定义")
.setView(custonView)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String userName=zh.getText().toString().trim();
String userPassword=pwd.getText().toString().trim();
Toast.makeText(MainActivity.this, userName+":"+userPassword, Toast.LENGTH_LONG).show();
}
})
.show();
}
}
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:"
android:textSize="20sp"/>
android:id="@+id/et_zh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="20sp"/>
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="请输入密码"/>
android:id="@+id/et_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"/>