Dialog 之自定义对话框
定义自己的对话框在很多地方都有使用,功能强大。
本次主要实现弹出登录界面的功能,效果如下:
1.首先在布局文件中添加一个按钮,实现点击之后弹出对话框。
2.新建一个布局文件,主要是弹出对话框时使用,代码如下:
<?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" >
<ImageView
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name"
android:scaleType="center" />
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:hint="用户名"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:fontFamily="sans-serif"
android:hint="密码"
android:inputType="textPassword" />
</LinearLayout>
3.主要在Activity中实现相关的功能:
package com.example.android_customdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) this.findViewById(R.id.button1);
// 注册监听事件
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
// 加载布局
View view = LayoutInflater.from(MainActivity.this).inflate(
R.layout.custom, null);
builder.setView(view);
final EditText username = (EditText) view
.findViewById(R.id.username);
final EditText password = (EditText) view
.findViewById(R.id.password);
// 设置确定按钮
builder.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
//获取所填的数据,并以Toast形式展示
String a = username.getText().toString().trim();
String b = password.getText().toString().trim();
Toast.makeText(
MainActivity.this,
"username:" + a + "\n"
+ "password:" + b, 0)
.show();
}
});
// 设置取消按钮
builder.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "取消操作", 0)
.show();
}
});// 设置取消
// 调用show方法
builder.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
要注意调用布局文件的方法。