代码小知识

来源:互联网 发布:淘宝美工职责 编辑:程序博客网 时间:2024/05/16 21:21

一:得到屏幕的宽度(两种方法):

1:

  DisplayMetrics dm = new DisplayMetrics();
  getWindowManager().getDefaultDisplay().getMetrics(dm);
  dm.widthPixels获取屏幕宽
  dm.heightPixels获取屏幕高度
2:
 screenWidth = context.getWindowManager().getDefaultDisplay().getWidth() 
 screenHeightt = context.getWindowManager().getDefaultDisplay().getHeight()

二:获得日期

public String refFormatNowDate() {

  Date nowTime = new Date(System.currentTimeMillis());
  SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");
  String retStrFormatNowDate = sdFormatter.format(nowTime);

  return retStrFormatNowDate;
}


三,动态设置布局:
  LinearLayout.LayoutParams vl = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                 //   vl.setMargins(Constants.dip2px(mContext,Constants.px2dip(mContext,20)), 0, Constants.dip2px(mContext,Constants.px2dip(mContext,50)), 0);
                    vl.setMargins(Constants.dip2px(mContext,20),0,Constants.dip2px(mContext,30),0);
                    vl.gravity = Gravity.CENTER_VERTICAL;
                    textView.setLayoutParams(vl);
                    textView.setGravity(Gravity.CENTER);
                   // textView.setPadding(Constants.dip2px(mContext,Constants.px2dip(mContext,38)),0,Constants.dip2px(mContext,Constants.px2dip(mContext,38)),0);

四:键盘的控制

        // 键盘消失
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

        // 键盘出现
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.RESULT_SHOWN);

// 在点击事件中加入,如果键盘存在,让软键盘消失
try {
               ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(GroupChatActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }catch (Exception e){
                    e.printStackTrace();
                }
 在清单文件中添加,是页面在一开始不显示软键盘:(有问题)
  android:windowSoftInputMode="stateHidden"

默认不显示软键盘:

在oncreate方法里面加-------》
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);


五:验证时间倒计时

        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mBtn.setEnabled(false);
                mBtn.post(new Runnable() {
                    @Override
                    public void run() {
                        mBtn.setText("剩余时间"+time+"秒");
                        if(time>1){
                            time--;
                            mBtn.postDelayed(this,1000);
                        }else{
                            time = 20;
                        }

                    }
                });

            }
        });


    六:控制一个控件的消失和显示
boolean visible = mMoreLayout.getVisibility() == View.VISIBLE;       
  view.setVisibility(visible ? View.GONE : View.VISIBLE);


七: 弹框
/**
     * 弹出右上角号码群操作窗口
     */
    private void showGroupOpreatePop(){
        final View mainView = mInflater.inflate(R.layout.cloud_group_operation_pop_layout,null);
        LinearLayout btnCreateCircle = (LinearLayout) mainView.findViewById(R.id.ll_create_circle);
        LinearLayout btnCreateYellowPage = (LinearLayout) mainView.findViewById(R.id.ll_create_yellowpage);
        TextView btnSubscribeGroup = (TextView) mainView.findViewById(R.id.tv_subscribe_group);
        btnCreateCircle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOperatePopWindow.dismiss();
            }
        });
        btnCreateYellowPage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOperatePopWindow.dismiss();
            }
        });
        btnSubscribeGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOperatePopWindow.dismiss();
                Intent intent = new Intent(mActivity, SubscribeListActivity.class);
                startActivity(intent);
            }
        });
        int popWidth = Constants.dip2px(mActivity,180);
        int popHeight = Constants.dip2px(mActivity,168);
        mOperatePopWindow = new PopupWindow(mainView,
                popWidth,
                popHeight);
        mOperatePopWindow.setFocusable(true);
        mOperatePopWindow.setOutsideTouchable(true);
        int xOffSet = mActivity.getWindowManager().getDefaultDisplay().getWidth() - popWidth - 16;
        int yOffSet = Constants.dip2px(mActivity,14);
        mOperatePopWindow.setBackgroundDrawable(new ColorDrawable(
                Color.TRANSPARENT));
        mOperatePopWindow.showAsDropDown(mTitleBar,xOffSet,-yOffSet);
        AlphaAnimation aa = new AlphaAnimation(0.4f, 1.0f);  
        aa.setDuration(250);
        ScaleAnimation sa = new ScaleAnimation(0.2f, 1.0f, 0.2f,
                1.0f, Animation.RELATIVE_TO_SELF, 0.91f,
                Animation.RELATIVE_TO_SELF, 0f);
        sa.setDuration(250);
        AnimationSet set = new AnimationSet(false);
        set.addAnimation(aa);
        set.addAnimation(sa);
        mainView.startAnimation(set);
    }





0 0
原创粉丝点击