简单dialog---带输入框/确定/取消

来源:互联网 发布:阿里云 部署爬虫 编辑:程序博客网 时间:2024/05/01 16:34
package com.hzpd.tts.ui;import android.app.Dialog;import android.content.Context;import android.content.DialogInterface;import android.view.View;import android.view.ViewGroup.LayoutParams;import com.hzpd.tts.R;/** *  * Create custom Dialog windows for your application Custom dialogs rely on * custom layouts wich allow you to create and use your own look & feel. *  * Under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html *  * <a href="http://my.oschina.net/arthor" target="_blank" * rel="nofollow">@author</a> antoine vianey *  */public class CustomDialog extends Dialog {public CustomDialog(Context context, int theme) {super(context, theme);}public CustomDialog(Context context) {super(context);}/** * Helper class for creating a custom dialog */public static class Builder {private Context context;private String title;// private String message;private String tv_heiwei_sureText;private String tv_heiwei_cancelText;private View contentView;private DialogInterface.OnClickListener tv_heiwei_sureClickListener,tv_heiwei_cancelClickListener;private CustomDialog dialog;public Builder(Context context) {this.context = context;}/** * Set the Dialog title from resource *  * @param title * @return */public Builder setTitle(int title) {this.title = (String) context.getText(title);return this;}/** * Set the Dialog title from String *  * @param title * @return */public Builder setTitle(String title) {this.title = title;return this;}/** * Set a custom content view for the Dialog. If a message is set, the * contentView is not added to the Dialog... *  * @param v * @return */public Builder setContentView(View v) {this.contentView = v;return this;}/** * Set the positive button resource and it's listener *  * @param tv_heiwei_sureText * @param listener * @return */public Builder settv_heiwei_sure(String tv_heiwei_sureText,DialogInterface.OnClickListener listener) {this.tv_heiwei_sureText = tv_heiwei_sureText;this.tv_heiwei_sureClickListener = listener;return this;}/** * Set the negative button text and it's listener *  * @param tv_heiwei_cancelText * @param listener * @return */public Builder settv_heiwei_cancel(String tv_heiwei_cancelText,DialogInterface.OnClickListener listener) {this.tv_heiwei_cancelText = tv_heiwei_cancelText;this.tv_heiwei_cancelClickListener = listener;return this;}/** * Create the custom dialog */public CustomDialog create(View layout) {dialog = new CustomDialog(context, R.style.Theme_Transparent);dialog.setCanceledOnTouchOutside(false);dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));dialog.setContentView(layout);return dialog;}}}




// 在页面中加入

private void addDialog() {CustomDialog.Builder customBuilder = new CustomDialog.Builder(HeightWeightActivity.this);LayoutInflater inflater = (LayoutInflater) HeightWeightActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View layout = inflater.inflate(R.layout.dialog_height_weight, null);dialog = customBuilder.create(layout);TextView tv_heiwei_cancel = (TextView) layout.findViewById(R.id.tv_heiwei_cancel);TextView tv_heiwei_sure = (TextView) layout.findViewById(R.id.tv_heiwei_sure);et_myheight = (EditText) layout.findViewById(R.id.et_myheight);et_myweight = (EditText) layout.findViewById(R.id.et_myweight);tv_heiwei_cancel.setOnClickListener(this);tv_heiwei_sure.setOnClickListener(this);dialog.show();dialog.getWindow().setLayout(width, LayoutParams.WRAP_CONTENT); // 设置Dialog大小要在show()之后否则没效果}

// xml : 

<?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:layout_marginBottom="10dp"    android:layout_marginLeft="10dp"    android:layout_marginRight="10dp"    android:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:layout_marginRight="10dp"        android:background="@drawable/dialog_bg"        android:orientation="vertical" >        <TextView            android:layout_width="match_parent"            android:layout_height="50dp"            android:gravity="center"            android:text="请输入"            android:textColor="#4c4c4c"            android:textSize="16sp" />        <View            android:layout_width="match_parent"            android:layout_height="1dp"            android:background="#f1f1f1" />        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal"            android:orientation="horizontal" >            <TextView                android:layout_width="match_parent"                android:layout_height="50dp"                android:gravity="center"                android:text="身高:"                android:textColor="#4c4c4c"                android:textSize="16sp" />            <EditText                android:id="@+id/et_myheight"                android:layout_width="70dp"                android:layout_height="30dp"                android:layout_marginLeft="15dp"                android:layout_marginRight="15dp"                android:background="@drawable/et_danbai_bg"                android:numeric="decimal"                android:singleLine="true" />            <TextView                android:layout_width="match_parent"                android:layout_height="50dp"                android:gravity="center"                android:text="cm"                android:textColor="#4c4c4c"                android:textSize="16sp" />        </LinearLayout>        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal"            android:orientation="horizontal" >            <TextView                android:layout_width="match_parent"                android:layout_height="50dp"                android:gravity="center"                android:text="体重:"                android:textColor="#4c4c4c"                android:textSize="16sp" />            <EditText                android:id="@+id/et_myweight"                android:layout_width="70dp"                android:layout_height="30dp"                android:layout_marginLeft="15dp"                android:layout_marginRight="15dp"                android:background="@drawable/et_danbai_bg"                android:numeric="decimal"                android:singleLine="true" />            <TextView                android:layout_width="match_parent"                android:layout_height="50dp"                android:gravity="center"                android:text="kg"                android:textColor="#4c4c4c"                android:textSize="16sp" />        </LinearLayout>        <View            android:layout_width="match_parent"            android:layout_height="1dp"            android:background="#f1f1f1" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="50dp"            android:orientation="horizontal" >            <TextView                android:id="@+id/tv_heiwei_cancel"                android:layout_width="0dp"                android:layout_height="50dp"                android:layout_weight="1"                android:gravity="center"                android:text="取消"                android:textColor="#4c4c4c"                android:textSize="16sp" />            <TextView                android:id="@+id/tv_heiwei_sure"                android:layout_width="0dp"                android:layout_height="50dp"                android:layout_weight="1"                android:gravity="center"                android:text="确定"                android:textColor="#19c0be"                android:textSize="16sp" />        </LinearLayout>    </LinearLayout></LinearLayout>


0 0