EditText 相关知识点集锦

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

获得焦点即全选

代码: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"

如:

<RelativeLayout
  style="@style/TitleTopBackgroundStyle"
  android:focusable="true"
  android:focusableInTouchMode="true"
  >

  <ImageView
      style="@style/TitleTopBackStyle"
      android:src="@mipmap/back" />

  <!--搜索框-->
  <EditText
      android:id="@+id/tv_search_widget"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:layout_marginRight="10dp"
      android:background="@drawable/shape_rect_search_background"
      android:hint="搜索书籍"
      android:paddingBottom="5dp"
      android:paddingLeft="30dp"
      android:paddingRight="70dp"
      android:paddingTop="5dp"
      android:textColorHint="@color/White"
      android:textSize="14dp" />

  <ImageView
      android:layout_width="25dp"
      android:layout_height="25dp"
      android:layout_alignLeft="@id/tv_search_widget"
      android:layout_centerVertical="true"
      android:paddingLeft="10dp"
      android:src="@mipmap/search" />
</RelativeLayout>

四、判断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);
// 设置hint
editText.setHint(new SpannedString(ss)); // 一定要进行转换,否则属性会消失

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

除了可以改变Hint的大小,其它属性都可更改, 具体的的Spaned类型可以参考这个链接: Android中的各类Span全面系统研究

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

xml文件

<EditText
	android:id="@+id/editTxt_userName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="搜索书籍"
    android:layout_alignParentRight="true"
    android:drawableLeft="@mipmap/search"
    />

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
    android:id="@+id/editTextShow"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hint"
    android:cursorVisible="false"
    android:lines="1"
    android:maxLength="16"
    />

方法二:在代码中控制

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

以上是在日常工作中遇到的EditText相关问题做的笔记,其中参考了很多文章,时间久远找不到各个原文地址了,见谅!
原作者如果看到可以提醒我加上原文链接~

猜你喜欢

转载自blog.csdn.net/solocoder/article/details/83656001