Skip to content Skip to sidebar Skip to footer

Android Edittext Onclick Event Handling And Selection

I have two edit text views. If I click first, I need to select first edittext and set to second '00'. Like in default android alarm clock. My problem: I have api level 10, so I ca

Solution 1:

If I understood correctly, you want to do the following:

  • When focusing firstEText, select all the text within firstEText and set secondEText to "00".

What I don't understand is why you say you cannot use setOnFocusChangeListener, since, it is available since API 1.

A convenient attribute to select all the text of an EditText when getting focus on an element, is android:selectAllOnFocus, which does exactly what you want. Then, you just need to set secondEText to "00".

UI

<EditText
    android:id="@+id/editText1"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:selectAllOnFocus="true"
    android:background="@android:color/white"
    android:textColor="@android:color/black" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@android:color/white"
    android:textColor="@android:color/black" />

Activity

firstEText = (EditText) findViewById(R.id.editText1);
secondEText = (EditText) findViewById(R.id.editText2);

firstEText.setOnFocusChangeListener(newView.OnFocusChangeListener() {

    @OverridepublicvoidonFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            secondEText.setText("00");
        }
    }

});

Hope it helps.

Post a Comment for "Android Edittext Onclick Event Handling And Selection"