本文共 4311 字,大约阅读时间需要 14 分钟。
获得焦点即全选
代码:edit.setSelectAllOnFocus(true);
xml配置:Android:selectAllOnFocus="true"
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的父控件设置这两个属性:
android:focusable="true" android:focusableInTouchMode="true"
如:
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(); }
今天同事在工作中碰到一个问题, 就是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类型可以参考这个链接:
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的Text中的输入长度呢?
比如:
editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(20)});
以上是在日常工作中遇到的EditText相关问题做的笔记,其中参考了很多文章,时间久远找不到各个原文地址了,见谅!
原作者如果看到可以提醒我加上原文链接~转载地址:http://jvvpi.baihongyu.com/