/*
* 登入对话框
* 在xml文件里我们定义了三个输入框,一个是用户名输入框、
* 一个是密码输入框另一个是电话号码输入框,下面我们就在
* 应用程序中调用AlertDialog.Builder的setView(View view)
* 方法让对话框显示该输入界面
*/
import 略
public class Ex02_11Activity extends Activity {
private Button bt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt = (Button) findViewById(R.id.bt);
final Builder builder = new AlertDialog.Builder(this);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stu
TableLayout loginForm = (TableLayout) getLayoutInflater()
.inflate(R.layout.loginlayout, null);
// 设置对话框图标和标题
builder.setIcon(R.drawable.tools)
.setTitle("自定义对话框")
.setView(loginForm)//这里是为对话框添加布局文件
.setPositiveButton("登入", new OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
// 此处填写点 击登 入按钮后处理的事件
}
})
.setNeutralButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
// 此处填写点击 取消 按钮后处理的事件
}
}).show();
}
});
}
}
下面我们就来看看两个布局文件main.xml和loginlayout.xml。
main.xml文件其实很简单:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal|center"
android:text="@string/hello"
android:textSize="20dp" />
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="@string/regist" />
</LinearLayout>
loginlayout.xml文件code
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loginForm"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/username" />
<!-- 输入用户名文本框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/hintUser"
android:selectAllOnFocus="true" />
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/username" />
<!-- 输入密码文本框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/hintPwd"
android:inputType="textPassword" />
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/phone" />
<!-- 输入电话号码文本框 -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:selectAllOnFocus="true" />
</TableRow>
</TableLayout>
下面我们来看下程序运行后的效果:
完..............................................