2018.08.06[android日心得]

日工作總結

  • 1.根據接口獲取數據是否爲空,對獲取集合元素是否爲空進行判斷
    代碼如下


    private void getIndex() {
    String ua = PermissionUtil.getInstance().getImei(activity);
    String version = activity.getResources().getString(R.string.version_name);
    action.getIndex(activity, ua, version, new ActionCallbackListener() {
    @Override
    public void onSuccess(IndexBean data) {

    //註釋:data.getArticles().size()是對接口獲取的數組數據進行判斷.size()是否爲空,爲空的話setText("XX"),否則獲取接口獲取的數據.
            if (data.getArticles().size()==0){
                tv_announce.setText("無數據");
            }else {               tv_announce.setText(data.getArticles().get(0).getTitle());
            }
    //註釋:獲取Bean中數據進行數據展示
            List<OptimizationProductsBean> list = new ArrayList<>();
    
            for (int i = 0; i < data.getOptimizationProducts().size(); i++) {
    
                data.getOptimizationProducts().get(i).getImageUrl();
    
                list.add(data.getOptimizationProducts().get(i));
            }
    
            FavorAdapter adapter = new FavorAdapter(activity, list);
            lv_favor.setAdapter(adapter);
            //註釋:根據listview的item個數,動態設置listview的高度
            setListViewHeightBasedOnChildren(lv_favor);
        @Override
        public void onFailure(int errorCode, String message) {
            RequestBackCodeUtil.getInstance().handle(activity, errorCode, message);
        }
    });
    
  • 2.根據item數動態設定ListView的高度

  • //註釋:此方法在setAdapter之後調用,子ListView的每個Item必須是LinearLayout,不能是其他的,因爲其他的Layout(如RelativeLayout)沒有重寫onMeasure(),所以會在onMeasure()時拋出異常。

    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
         return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
         View listItem = listAdapter.getView(i, null, listView);
         listItem.measure(0, 0);
         totalHeight += listItem.getMeasuredHeight();
        }
    
        ViewGroup.LayoutParams params = listView.getLayoutParams();
    
        params.height = totalHeight
          + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
     }
    
  • 3.activity向Fragment傳值


    //註釋:在activity中創建方法,便於在Fragment中取值
    public AppAction getAppAction(){
    return appAction;
    }
    public MyApplication application(){
    return application;
    }
    //註釋:在Fragment中onAttach()方法中獲取activity傳來的值
    @Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
    aAction = ((PosterActivity) activity).getAppAction();
    mApplication = ((PosterActivity) activity).getApplication();
    }

  • 4.activity向Fragment傳值


    獲取SharedPreferences的兩種方式:
    1 調用Context對象的getSharedPreferences()方法
    2 調用Activity對象的getPreferences()方法
    兩種方式的區別:
    調用Context對象的getSharedPreferences()方法獲得的SharedPreferences對象可以被同一應用程序下的其他組件共享.
    調用Activity對象的getPreferences()方法獲得的SharedPreferences對象只能在該Activity中使用.
    
    SharedPreferences的四種操作模式:
    Context.MODE_PRIVATE
    Context.MODE_APPEND
    Context.MODE_WORLD_READABLE
    Context.MODE_WORLD_WRITEABLE
    
    Context.MODE_PRIVATE:爲默認操作模式,代表該文件是私有數據,只能被應用本身訪問,在該模式下,寫入的內容會覆蓋原文件的內容
    Context.MODE_APPEND:模式會檢查文件是否存在,存在就往文件追加內容,否則就創建新文件.
    Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來控制其他應用是否有權限讀寫該文件.
    MODE_WORLD_READABLE:表示當前文件可以被其他應用讀取.
    MODE_WORLD_WRITEABLE:表示當前文件可以被其他應用寫入.
    將數據保存至SharedPreferences:
    SharedPreferences preferences=getSharedPreferences("user",Context.MODE_PRIVATE);
    Editor editor=preferences.edit();
    String name="xixi";
    String age="22";
    editor.putString("name", name);
    editor.putString("age", age);
    editor.commit();
    
    從SharedPreferences獲取數據:
    SharedPreferences preferences=getSharedPreferences("user", Context.MODE_PRIVATE);
    String name=preferences.getString("name", "defaultname");
    String age=preferences.getString("age", "0");
    
  • 5.android中自定義彈出框樣式


    private void showDialog() {
            final Dialog dialog = new Dialog(AppointMentActivity.this, R.style.dialogTheme);
            //View dialogView = LayoutInflater.from(activity).inflate(R.layout.photo_select,null);
            View dialogView = LayoutInflater.from(AppointMentActivity.this).inflate(R.layout.dialog_yuyue,null);
            //獲得dialog的window窗口
            Window window = dialog.getWindow();
            //設置dialog在屏幕底部
            window.setGravity(Gravity.CENTER);
            //設置dialog彈出時的動畫效果,從屏幕底部向上彈出
            // window.setWindowAnimations(R.style.dialogStyle);
            window.getDecorView().setPadding(DensityUtil.dip2px(30), DensityUtil.dip2px(30), DensityUtil.dip2px(30), DensityUtil.dip2px(30));
            //獲得window窗口的屬性
            WindowManager.LayoutParams lp = window.getAttributes();
            //設置窗口寬度爲充滿全屏
            lp.width = DensityUtil.dip2px(300);
            //設置窗口高度爲包裹內容
            // lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.height = DensityUtil.dip2px(300); // 設置 dialog 高度
            //將設置好的屬性set回去
            window.setAttributes(lp);
            //將自定義佈局加載到dialog上
            dialog.setContentView(dialogView);
            //dialog.setCanceledOnTouchOutside(false); // false 調用這個方法時,按對話框以外的地方不起作用。按返回鍵還起作用
            dialog.setCanceledOnTouchOutside(true);
    
            MyTextView tv_confirm = dialogView.findViewById(R.id.tv_confirm);
            tv_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
    
            dialog.show();
        }
    
  • 6.點擊提交進行數據校驗


    //註釋:在調接口數據之前進行驗證
     @Override
      public void onClick(View v) {
            switch (v.getId()){
                case R.id.rl_subscribe:
                    if(TextUtils.isEmpty(et_name.getText().toString())){
                        ToastUtil.show(AppointMentActivity.this,"請輸入姓名");
                        return;
                    }
    //註釋:先爲匹配正則表達式
                    String nameFormula = "[\\u4e00-\\u9fa5a-zA-Z]{2,50}"; // 2-50字中英文姓名
                    if(!et_name.getText().toString().matches(nameFormula)){
                        ToastUtil.show(context,"請輸入2-50中文或是英文");
                        return;
                    }
                    if(TextUtils.isEmpty(et_phone.getText().toString()) || et_phone.getText().toString().length() != 11){
                        ToastUtil.show(AppointMentActivity.this,"手機號格式錯誤,請重新輸入");
                        return;
                    }
                    if(et_input.getText().toString().length() > 30){
                        ToastUtil.show(AppointMentActivity.this,"備註最多30個字");
                        return;
                    }
                    showDialog();
                    getAppointment();
                    break;
                case R.id.base_back:
                    break;
    
            }
            super.onClick(v);
        }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章