Reputation: 988
I'm trying to get a phone number but the EditText
doesn't show the numeric digit and the String t
is empty. If I start to enter a + sign followed by some numeric value I get only + in edit.gettext().toString()
.
If I set inputtype="text"
everything works as normal.
Is it a bug or what?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.narb.test.MainActivity">
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Text typed"
/>
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:hint="Enter text"
android:inputType="phone"
/>
</RelativeLayout>
Here is my code:
public class MainActivity extends AppCompatActivity {
TextView txt;
EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) findViewById(R.id.edit);
txt = (TextView) findViewById(R.id.txt);
edit.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String t = edit.getText().toString();
txt.setText(t);
}
return true;
}
});
}
}
Upvotes: 0
Views: 511
Reputation: 1159
If block will return true and write false at the end.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchview);
edit = (EditText) findViewById(R.id.edit);
txt = (TextView) findViewById(R.id.txt);
edit.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String t = edit.getText().toString();
txt.setText(t);
return true;
}
return false;
}
});
}
Upvotes: 0
Reputation: 1428
Try the following this worked for me.
You should only return true from onKey if you're handling the input. I.e., move the return true inside your if block and return false otherwise
edit.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String t = edit.getText().toString();
txt.setText(t);
return true;
}
return false;
}
});
Upvotes: 1
Reputation: 2085
I guess you should try this this
android:inputType="number"
As Input type "number" is that the keyboard allows to switch over to characters and also lets other special characters be entered.
Upvotes: 0