android 取消edittext焦點

頁面中若是有EditText會默認獲取焦點,若是進入頁面時不想讓其獲取到焦點能夠按以下步驟:android

 

一、在佈局的最外層添加屬性:ide

android:focusable="true"
android:focusableInTouchMode="true"佈局

二、給最外層佈局添加一個標記:xml

<requestFocus />事件

 

效果爲:get

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:focusable="true"
  android:focusableInTouchMode="true" >it


  <requestFocus />io

  <EditText
    android:id="@+id/et_content"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />event

</RelativeLayout>List

 

若是還想在點擊EditText之外的區域時讓其失去焦點,能夠給佈局添加一個點擊事件:

contentView.setOnTouchListener(new OnTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){
      et_content.clearFocus();
    }
    return false;
  }
});

//contentView就上面的那個佈局文件

//View contentView = View.inflate(getApplicationContext(),R.layout.activity_xx, null);

//et_content是你的EditText

相關文章
相關標籤/搜索