Android Use EditText Click Select All again click Cancel All select Feature _android

Source: Internet
Author: User
Tags gety xmlns

Recently in the development browser encountered such a demand: Click on the Address bar, you need to select all and bring up the keyboard, click again to cancel the full selection of the display cursor. Click on the screen in addition to other locations in the address bar, the keyboard hidden, hide the cursor.

Most browsers are such logic, which can improve the user experience and reduce the operation.

The code is simple, here I simplify the logic, the page has only one edittext.

The layout files are as follows: There are two attributes in it that need attention.

Android:focusable= "true"
android:selectallonfocus= "true"

Full Layout file

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
  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 ">
  <edittext
    android:id=" @+id/edit "
    android:layout_ Width= "Match_parent"
    android:layout_height= "wrap_content"
    android:focusable= "true"
    Android: Selectallonfocus= "true"
    />
</RelativeLayout>

**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, Motioneve NT Motionevent) {if (motionevent.getaction () = = motionevent.action_up) {Edittext.setfocusableintouchmo
          De (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_metho
        D_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; }
}

Need to be aware of two code snippets

Edittext.setfocusableintouchmode (true);
Edittext.requestfocus ();

The above is a small set of Android to introduce you to the use of EditText click Select All again click Cancel All select function, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.