点击编辑框全选内容java_Android 中使用EditText 点击全选再次点击取消全选功能
最近在開發瀏覽器碰到這么一個需求:點擊地址欄的時候,需要全選并調出鍵盤,再次點擊就取消全選顯示光標。點擊屏幕除地址欄其他位置時,鍵盤隱藏,隱藏光標。
大部分瀏覽器都是這樣的邏輯,這樣可以提高用戶體驗,減少操作。
代碼很簡單,這里我簡化了邏輯,頁面只有一個EditText。
布局文件如下:里面有兩個屬性需要注意
android:focusable="true"
android:selectAllOnFocus="true"
完整布局文件
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.edittexttest.MainActivity">
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:selectAllOnFocus="true"
/>
**mainactivity.java
package com.example.edittexttest;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edit);
editText.setText("click to select all");
editText.clearFocus();
editText.setFocusableInTouchMode(false);
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
editText.setFocusableInTouchMode(true);
editText.requestFocus();
editText.setText("click to select all");
editText.selectAll();
}
return false;
}
});
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
return super.dispatchTouchEvent(ev);
}
// Necessary
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
editText.clearFocus();
editText.setFocusableInTouchMode(false);
return onTouchEvent(ev);
}
public boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof EditText)) {
int[] leftTop = { 0, 0 };
//get location of TextView
v.getLocationInWindow(leftTop);
int left = leftTop[0];
int top = leftTop[1];
int bottom = top + v.getHeight();
int right = left + v.getWidth();
if (event.getX() > left && event.getX() < right
&& event.getY() > top && event.getY() < bottom) {
return false;
} else {
return true;
}
}
return false;
}
}
需要注意兩個代碼段
editText.setFocusableInTouchMode(true);
editText.requestFocus();
以上所述是小編給大家介紹的Android 中使用EditText 點擊全選再次點擊取消全選功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
總結
以上是生活随笔為你收集整理的点击编辑框全选内容java_Android 中使用EditText 点击全选再次点击取消全选功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zend studio php 5.5,
- 下一篇: mysql数据库管理维护_(转)Mysq