Android 开发中的一些小细节:Edittext,Button,Paint等

EditText:
hite多行文本时,让光标显示在左上角(默认中间),只要将gravity属性设置为top就可以了。

android:gravity="top"

在代码中设置EditText的字体颜色,刚开始使用的是:

EditText.setTextColor(R.color.colorEditNum);//无效代码

发现这样写是无效的,要修改成如下代码:

EditText.setTextColor(this.getResources().getColor(R.color.colorEditNum));

EditText获得焦点选中全部文本:

//修改布局文件:
android:selectAllOnFocus="true"

android点击EditText外区域收起键盘:

 boolean pendingCollapseKeyword = false;
 View focusedView;
    // 点击非输入框位置优先隐藏软键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            pendingCollapseKeyword = isShouldHideInput(ev);
            if (pendingCollapseKeyword) focusedView = getCurrentFocus();
        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            if (pendingCollapseKeyword) {
                hideInputMethod(this);
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private boolean isShouldHideInput(MotionEvent event) {

        View v = getCurrentFocus();
        if (v instanceof EditText) {
            int[] location = {0, 0};
            v.getLocationInWindow(location);
            return event.getX() < location[0]
                   || event.getX() > location[0] + v.getWidth()
                   || event.getY() < location[1]
                   || event.getY() > location[1] + v.getHeight();
        }
        return false;
    }

    // 抬起手指时如果焦点还在原来的EditText则收起键盘
    private void hideInputMethod(Context context) {

        View v = getCurrentFocus();
        if (v == focusedView) {
            focusedView.clearFocus();
            InputMethodManager imm = (InputMethodManager)context
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
            }
        }
    }

EditText获取焦点时的操作以及失去焦点时操作,如下:

editText.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener() {  
    @Override  
    public void onFocusChange(View v, boolean hasFocus) {  
        if(hasFocus) {
              // 此处为得到焦点时的处理内容
        } else 
        {
             // 此处为失去焦点时的处理内容
        }
    }
});

判断EditText中输入为空时:

❌:editText.getText().toString().equals("")
❌:editText.getText().toString().equals(null)
❌:editText.getText().toString().equals("null")
❌:editText.getText().toString() == "null"
在java里面String是不能用 ==!= 比较的,要用equals()

✅:editText.getText().toString().equals("")

Button:
去除按钮默认的点击效果,只需要在.xml中加入下面这行代码即可。

android:stateListAnimator="@null"

或者:

style="?android:attr/borderlessButtonStyle"

Android 浮点型当小数点后为0时自动去掉:

NumberFormat nf = new DecimalFormat("#.###");
float a = 3.10f;
System.out.println(nf.format(a));
//输出3.1

Paint:
(为字体类型设置字体风格,如设置粗体:)

Paint mp = new Paint();
Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
p.setTypeface( font );

常用的字体风格名称还有:

  * Typeface.BOLD //粗体
  * Typeface.BOLD_ITALIC //粗斜体
  * Typeface.ITALIC //斜体
  * Typeface.NORMAL //常规

(以此文章记录开发中遇到的坑,本文持续更新中。。。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值