android 通用view封装

来源:互联网 发布:linux 套接字文件 编辑:程序博客网 时间:2024/05/18 16:39
<pre style="background-color: rgb(255, 255, 255); font-family: 'Courier New'; font-size: 15pt;"><span style="color: rgb(0, 0, 128);"><strong>分装某个view 实现通用界面和点击事件等</strong></span>

public class PayResultView extends RelativeLayout {    private Context context;    private ImageView logo;    private TextView pay_result_tips;    private TextView company_phone;    private Button confirm;    private TextView worktime;    private String ok_tips;    private String worktime_tips;    private String company_phone_tips;    private String error_tips;    private String def_work_week = "周一至周五";    private String def_work_time = "9:00-18:00";    private String def_phont_number = "010-53317783";    public PayResultView(Context context) {        super(context);        init(context);    }    public PayResultView(Context context, AttributeSet attrs) {        super(context, attrs);        init(context);    }    public PayResultView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init(context);    }    private void init(Context context) {        this.context = context;        LayoutInflater.from(context).inflate(R.layout.pay_result, this, true);        logo = (ImageView) findViewById(R.id.pay_result_logo);        confirm = (Button) findViewById(R.id.confirm_pay);        worktime = (TextView) findViewById(R.id.work_time);        company_phone = (TextView) findViewById(R.id.company_phone);        pay_result_tips = (TextView) findViewById(R.id.pay_result_tips);        ok_tips = context.getResources().getString(R.string.pay_result_ok_tips);        error_tips = context.getResources().getString(R.string.pay_result_error_tips);        company_phone_tips = context.getResources().getString(R.string.pay_result_connect);        worktime_tips = context.getResources().getString(R.string.pay_result_worktime);        getCompanyInfo();    }    private void getCompanyInfo() {        HttpUtils httpUtils = new HttpUtils();        httpUtils.send(HttpRequest.HttpMethod.GET, URLs.SERVICE_INFO, new RequestCallBack<String>() {            @Override            public void onSuccess(ResponseInfo<String> responseInfo) {                ResultBean resultBean = JSON.parseObject(responseInfo.result, ResultBean.class);                if (null != resultBean && ResultBean.STATUS_SUCCESS.equals(resultBean.http_status)) {                    try {                        JSONObject object = new JSONObject(resultBean.data);                        String work_week = (String) object.get("work_week");                        String phone_number = (String) object.get("phone_number");                        String work_time = (String) object.get("work_time");                        setInfo(work_week, phone_number, work_time);                    } catch (JSONException e) {                        LogUtils.e("text", "JSONException in parseing servicesinfo");                        setInfo(def_work_week, def_phont_number, def_work_time);                    }                }            }            @Override            public void onFailure(HttpException e, String s) {                setInfo(def_work_week, def_phont_number, def_work_time);            }        });    }    private void setInfo(String work_week, String phone_number, String work_time) {        company_phone.setText(String.format(company_phone_tips, phone_number));        worktime.setText(String.format(worktime_tips, new StringBuffer(work_week).append(" ").append(work_time).toString()));    }    /**     * 设置支付结果     *     * @param money  如果支付成功,把金额以字符串形式传进来,失败则填null     * @param success 支付成功true,失败false     */    public void setResult(String money, boolean success) {        if (success) {            logo.setBackgroundResource(R.drawable.confirm_green);            pay_result_tips.setText(String.format(ok_tips, money));        } else {            logo.setBackgroundResource(R.drawable.icon_fail);            pay_result_tips.setText(error_tips);        }    }    /**     * 设置点击事件     *     * @param listener     */    public void setButtonClicklistenter(View.OnClickListener listener) {        confirm.setOnClickListener(listener);    }}


                                             
0 0
原创粉丝点击