您的位置:首页 > 移动开发 > Android开发

自定义对话框

2015-10-02 00:15 459 查看


1、效果图



2、布局文件

主布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"

>
<Button
android:id="@+id/bt_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点我"
android:layout_centerInParent="true"
/>

</RelativeLayout>


对话框布局

<?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:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/blue_bkg"
android:gravity="center"
android:text="添加黑名单"
android:textColor="#ffffff"
android:textSize="26sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入黑名单号码"
android:inputType="phone"
android:textColor="#000000"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<CheckBox
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000000"
android:text="短信拦截"
/>
<CheckBox
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000000"
android:text="电话拦截"
/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
android:background="@drawable/blue_bkg"
android:text="添加"
/>
<Button
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
android:background="@drawable/blue_bkg"
android:text="取消"
/>

</LinearLayout>
</LinearLayout>


3、Activity

public class MainActivity extends Activity {

private Button bt_show;
private AlertDialog mAlertDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

initView();
initEvent();
}

/**
* 初始化按钮点击事件
*/
private void initEvent() {
bt_show.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
mAlertDialog.show();
}
});
}

/**
* 初始化界面、控件
*/
private void initView() {
setContentView(R.layout.activity_main);

bt_show = (Button) findViewById(R.id.bt_show);

// 通过对话框添加黑名单数据
AlertDialog.Builder ab = new AlertDialog.Builder(this);
// 自定义view
View mDialogView = View.inflate(getApplicationContext(),
R.layout.dialog, null);
//设置界面
ab.setView(mDialogView);
mAlertDialog = ab.create();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android 自定义View