CommonDialog

7071 ワード

実際の開発では、私たちのアプリプロジェクトの様々なヒントボックスが統一されています.コードとスタイルの重複を避けるために、統一スタイルのプロンプトボックスを定義する必要があります.コードの例は次のとおりです.
package com.wly.home.widget;

import com.wanbu.dascom.R;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
/** *      Dialog * @author wangly * */
public class CommonDialog extends Dialog implements android.view.View.OnClickListener {

    private TextView tv_title;
    private TextView tv_info;
    private EditText et_inputInfo;
    private TextView tv_number;
    private TextView tv_cancle;
    private TextView tv_ok;




    private CallBackListener listener;


    public void setListener(CallBackListener listener) {
        this.listener = listener;
    }

    public CommonDialog(Context context) {
        super(context,R.style.commonDialog_style);
        intView();
    }

    private void intView() {
        setContentView(R.layout.common_layout_dialog);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_info = (TextView) findViewById(R.id.tv_info);
        et_inputInfo = (EditText) findViewById(R.id.et_inputInfo);
        et_inputInfo.addTextChangedListener(watcher);
        tv_number = (TextView) findViewById(R.id.tv_number);
        tv_cancle = (TextView) findViewById(R.id.tv_cancle);
        tv_ok = (TextView) findViewById(R.id.tv_ok);
        tv_cancle.setOnClickListener(this);
        tv_ok.setOnClickListener(this);
    }
    /** *      * @param text * @param flag   /   */
    public void setTiltle(String text,int flag){
        tv_title.setText(text);
        tv_title.setVisibility(flag);
    }
    /** *   Dialog     * @param text * @param flag   /   */
    public void setInfo(String text,int flag){
        tv_info.setText(text);
        tv_info.setVisibility(flag);
    }

    /** *    "  "      * @param text */
    public void setCancleText(String text){
        tv_cancle.setText(text);
    }
    /** *    "  "      * @param text */
    public void setConfirmText(String text){
        tv_ok.setText(text);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.tv_cancle:
            if (listener!=null) {
                listener.cancleListener();
            }
            break;
        case R.id.tv_ok:
            if (listener!=null) {
                listener.confirmListener(et_inputInfo.getText().toString().trim());
            }
            break;
        default:
            break;
        }
    }
    /** * EditText      */
    int num = 100;//        
    TextWatcher watcher =new  TextWatcher(){
        private CharSequence temp;
        private int selectionStart;
        private int selectionEnd; 
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            temp = s; 
        }

        @Override
        public void afterTextChanged(Editable s) {
            int number = num - s.length();
            tv_number.setText(s.length()+"/100");
            selectionStart = et_inputInfo.getSelectionStart();
            selectionEnd = et_inputInfo.getSelectionEnd();
            if (temp.length() > num) {
                s.delete(selectionStart - 1, selectionEnd);
                int tempSelection = selectionEnd;
                et_inputInfo.setText(s);
                et_inputInfo.setSelection(tempSelection);//       
            }
        }

    };

    public interface CallBackListener{
        /** *    */
        void cancleListener();
        /** *    *   EditText       */
        void confirmListener(String text);
    }
}

<style name="commonDialog_style" parent="@android:Theme.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowBackground">@drawable/update_dialog_shape</item>

    </style>