Reputation: 379
I have an EditText as a text field to accept a telephone number amongst other personal details on an activity.
On activity launch the EditText is disabled until the user selects edit to edit the information. It is then enabled and allows the user to type a number in it.
Once the user clicks to save the data the EditText is made unfocusable using
txtPhone.setFocusable(false);
It then uses an onClickListener to allow the user to click it and call the number it contains.
When the user clicks edit to edit the number, I am setting
txtPhone.setFocusable(true);
To allow the EditText to receive focus however it is still seems to be using the onClickListener and won't allow the text to be edited giving the warning in log cat:
TextView does not support text selection. Action mode canceled.
Code for the EditText listener:
txtPhone.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(txtPhone.isFocusable()==false)
{
if(txtPhone.getText().length() > 1)
{
// Call number
// Using Toast to show listeners working first
Toast.makeText(getApplicationContext(), "Calling... " + txtPhone.getText().toString(), Toast.LENGTH_LONG).show();
}
}
}
});
The EditText xml:
<EditText
android:id="@+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:enabled="false"
android:hint="Telephone..."
android:inputType="phone"
android:maxLength="11" />
How can I make it so that the text is editable like on first launch and ignore the listener but when not focusable call the number, I don't seem to be able to get it to work as desired
Upvotes: 0
Views: 208
Reputation: 5122
I wasn't able to reproduce the warning you were getting, but wasn't able to get the Toast to appear on pressing the textbox when not focused. I did try the following to replicate what you're trying to achieve. Hopefully I understood you correctly.
EditText
Instead of using enabled="false"
I used focusable="false"
. This will have a similar behaviour to enabled being false, but allows the onClickListener
of txtPhone
to fire.
<EditText
android:id="@+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="false"
android:hint="Telephone..."
android:inputType="phone"
android:maxLength="11" />
Listeners
Since you didn't show code for the buttons, I used a button called btnEdit
to switch between focuses. Instead of txtPhone.setFocusable(true);
I used txtPhone.setFocusableInTouchMode(true);
to properly get the edit text to be editable.
btnEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (txtPhone.isFocusable()) {
txtPhone.setFocusable(false);
} else {
txtPhone.setFocusableInTouchMode(true);
}
}
});
txtPhone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (txtPhone.isFocusable() == false) {
if (txtPhone.getText().length() > 1) {
// Call number
// Using Toast to show listeners working first
Toast.makeText(MainActivity.this, "Calling... " + txtPhone.getText().toString(), Toast.LENGTH_LONG).show();
}
}
}
});
So when I first start the activity, txtPhone
is not editable. When I click the edit button, txtPhone
becomes editable and I am able to enter numbers. Lastly, I click the edit button again to disable txtPhone
and when I press on it, the Toast appears.
Upvotes: 1