博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EditText 相关知识点集锦
阅读量:4122 次
发布时间:2019-05-25

本文共 4311 字,大约阅读时间需要 14 分钟。

一、默认获取焦点后全选文字

获得焦点即全选

代码:edit.setSelectAllOnFocus(true);
xml配置:Android:selectAllOnFocus="true"

二、自定义AlertDialog软件盘弹不出解决办法

AlertDialog.Builder builder = new AlertDialog.Builder(OrderDetailActivity.this);    final AlertDialog dialog = builder.create();    View view = LayoutInflater.from(OrderDetailActivity.this).inflate(R.layout.alert_dialog_confirm_price,null);    final EditText etOrderPrice = (EditText) view.findViewById(R.id.et_order_price_alert_dialog);    TextView adCancel = (TextView) view.findViewById(R.id.tv_cancel_alert_dialog);    TextView adConfirm = (TextView) view.findViewById(R.id.tv_confirm_alert_dialog);    //dialog.setView加上这句话软键盘就可以弹出来了    dialog.setView(getLayoutInflater().inflate(R.layout.alert_dialog_confirm_price,null));    dialog.show();    //延迟300毫秒弹出软件盘    Timer timer = new Timer();    timer.schedule(new TimerTask() {        public void run() {            InputMethodManager inputManager = (InputMethodManager) etOrderPrice.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);            inputManager.showSoftInput(etOrderPrice, 0);        }    }, 300);	//设置宽度    WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();    lp.width = Utils.dp2px(OrderDetailActivity.this,320) ;    dialog.getWindow().setContentView(view);    dialog.getWindow().setAttributes(lp);

三、让EditText默认不获取焦点

在EditText的父控件设置这两个属性:

android:focusable="true"   android:focusableInTouchMode="true"

如:

四、判断EditText输入的是数字字母或汉字

String text = edInput.getText().toString();   Pattern p = Pattern.compile("[0-9]*");    Matcher m = p.matcher(text);    if(m.matches() ){    	Toast.makeText(Main.this,"输入的是数字",Toast.LENGTH_SHORT).show();    }    p=Pattern.compile("[a-zA-Z]");   m=p.matcher(text);   if(m.matches()){   	Toast.makeText(Main.this,"输入的是字母", Toast.LENGTH_SHORT).show();   }   p=Pattern.compile("[\u4e00-\u9fa5]");   m=p.matcher(text);   if(m.matches()){   	Toast.makeText(Main.this,"输入的是汉字", Toast.LENGTH_SHORT).show();   }

五、改变hint文字大小和其他属性

今天同事在工作中碰到一个问题, 就是EditText中的文字在设定大小后, Hint文本由于太长导致在EditText中无法完整的显示, 所以问有没有单独设置Hint文本大小的选项. 在网上看了一下都没有这方面的介绍. 于是我看了下TextView的源码(EditText继承自TextView), 发现了一些端倪,如下:

Java

public final void setHint(CharSequence hint) {    mHint = TextUtils.stringOrSpannedString(hint);    if (mLayout != null) {        checkForRelayout();    }    if (mText.length() == 0) {        invalidate();    }    // Invalidate display list if hint is currently used    if (mEditor != null && mText.length() == 0 && mHint != null) {        mEditor.invalidateTextDisplayList();    }}

在方法的一开始就是对hint文本的转换.由于hint是CharSequence类型的, 说明有希望可以增加一些自定义属性, 我们再看TextUtils.stringOrSpannedString这个方法:

Java

public static CharSequence stringOrSpannedString(CharSequence source) {    if (source == null)        return null;    if (source instanceof SpannedString)        return source;    if (source instanceof Spanned)        return new SpannedString(source);    return source.toString();}

那么问题来了,我们只要传入的hint是SpannedString或者Spanned类型,就可以保持文本的自定义属性了吗? 答案是肯定的! 直接上代码:

Java

EditText editText = (EditText) rootView.findViewById(R.id.et);// 新建一个可以添加属性的文本对象SpannableString ss = new SpannableString("喝酒就要喝一斤!");// 新建一个属性对象,设置文字的大小AbsoluteSizeSpan ass = new AbsoluteSizeSpan(8,true);// 附加属性到文本ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);// 设置hinteditText.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失

注意最后一步,一定要进行转换, 类型不对会被转换为String对象,这样自定义的额属性就会丢失.

除了可以改变Hint的大小,其它属性都可更改, 具体的的Spaned类型可以参考这个链接:

六、限定EditText中drawableLeft图片的大小

xml文件

java代码

public class LoginActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.login_index);                 //控制登录用户名图标大小        EditText editText1 = (EditText) findViewById(R.id.editTxt_userName);        Drawable drawable1 = getResources().getDrawable(R.drawable.login_user);        drawable1.setBounds(0, 0, 40, 40);//第一0是距左边距离,第二0是距上边距离,40分别是长宽        editText1.setCompoundDrawables(drawable1, null, null, null);//只放左边   }}

七、Android中设定EditText的输入长度

如何限定Android的Text中的输入长度呢?

方法一:可以在layout xml中加上属性 android:maxLength

比如:

方法二:在代码中控制

editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});

以上是在日常工作中遇到的EditText相关问题做的笔记,其中参考了很多文章,时间久远找不到各个原文地址了,见谅!

原作者如果看到可以提醒我加上原文链接~

转载地址:http://jvvpi.baihongyu.com/

你可能感兴趣的文章
Vue动态生成el-checkbox点击无法选中的解决方法
查看>>
python __future__
查看>>
MySQL Tricks1
查看>>
python 变量作用域问题(经典坑)
查看>>
pytorch
查看>>
pytorch(二)
查看>>
pytorch(三)
查看>>
pytorch(四)
查看>>
pytorch(5)
查看>>
ubuntu相关
查看>>
C++ 调用json
查看>>
nano中设置脚本开机自启动
查看>>
动态库调动态库
查看>>
Kubernetes集群搭建之CNI-Flanneld部署篇
查看>>
k8s web终端连接工具
查看>>
手绘VS码绘(一):静态图绘制(码绘使用P5.js)
查看>>
手绘VS码绘(二):动态图绘制(码绘使用Processing)
查看>>
基于P5.js的“绘画系统”
查看>>
《达芬奇的人生密码》观后感
查看>>
论文翻译:《一个包容性设计的具体例子:聋人导向可访问性》
查看>>