This question already has answers here:
How do I handle ImeOptions' done button click?
(9 answers)
Closed 6 years ago.
I have an EditText that let me type in the an IP address:
In the xml:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/enterIP"
android:layout_weight="1"
android:onClick="enterIP"
android:inputType="textPhonetic"
android:imeOptions="actionDone"
/>
I used android:imeOptions="actionDone" so that the input box will disappear after I press Done.
In the Java:
public void enterIP(View view) {
EditText theIP = (EditText) findViewById(R.id.enterIP);
try {
myIP = theIP.getText().toString();
validIP = ipvalidator.validate(myIP);
} catch (NullPointerException e)
{
Log.d("Error", "Input address is NULL.");
}
Toast.makeText(getApplicationContext(), "New IP is " + myIP, Toast.LENGTH_LONG).show();
}
However, the problem with this is that when I pressed Done, myIP still holds the old value. Only when I touch the EditText to bring up the input again the value is updated.
So how can I make sure myIP will be updated when Done is pressed. ?
Thanks
try this
yourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//do something
myIP = theIP.getText().toString();
validIP = ipvalidator.validate(myIP);
}
return false;
}
});
Use this method:
theIP.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// Do whatever you want here
return true;
}
return false;
}
you should set onEditorActionListener for the EditText to implement the action you want to perform.
theIP.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
try {
myIP = theIP.getText().toString();
validIP = ipvalidator.validate(myIP);
} catch (NullPointerException e)
{
Log.d("Error", "Input address is NULL.");
}
return true;
}
return false;
}
});
Related
I have 4 editText, and I want to store all the values in String arrayList. I have declared my arrayList globally. I can get the correct size of my arrayList, but I can't get any String value. Here's my code:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId == EditorInfo.IME_ACTION_DONE)
{
//get the user name
String player1 = editText.getText().toString();
playerName.add(player1);
}
return false;
}
});
editText1.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId == EditorInfo.IME_ACTION_DONE)
{
//get the user name
playerName.add(editText.getText().toString());
}
return false;
}
});
editText2.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId == EditorInfo.IME_ACTION_DONE)
{
//get the user name
playerName.add(editText.getText().toString());
}
return false;
}
});
editText3.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if(actionId == EditorInfo.IME_ACTION_DONE)
{
//get the user name
playerName.add(editText.getText().toString());
}
return false;
}
});
First, correct the listener, you use the same editText everywhere when you get the text editText.getText().toString().
Note : You could update this to use 1 instance of listener but that a different question.
To read a List, use List.get(int) or iterate it like
for(int i = 0; i < list.size(); ++i){
String s = list.get(i);
...
}
or
for(String s : list){
...
}
Now, did you need to use a listener, or could you just get the value when you need it ? That your project but that's an idea. Because did you want to stack the value if you input a value twice in the same EditText?
playerName.get(index);
you can get the string object you need from ArrayList in this way
try this:
//declare global
ArrayList<EditText> editextList = new ArrayList<>();
ArrayList<String> editextValuesList = new ArrayList<>();
onCreate(){
//bind edittext and then
editextList.add(editText);
editextList.add(editText1);
editextList.add(editText2);
editextList.add(editText3);
//you can use below lines anywhere if any text changes in edittext then it will give you updated text
for(int i=0;i<edittextList.size();i++){
String editTextString = edittextList.get(i).getText().toString();
editextValuesList.add(editTextString);
}
}
To get a single player, please try this code:
String player1 = playerName.get(0);
To get all the players from the list, please use this code:
for (String player : playerName) {
Log.d("TAG", player);
}
Hope it helps.
I'm trying to cacth done action which comes from softkeyboard. My code is like this;
private TextView.OnEditorActionListener getDoneListener() {
return new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
nextPage();
return true;
}
else {
return false;
}
}
};
}
However even though IDE evaluates "(actionId == EditorInfo.IME_ACTION_DONE)"as true, it's not calling nextPage(), returning false instead. If I cast expression like this, it works fine.
private TextView.OnEditorActionListener getDoneListener() {
return new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((boolean)(actionId == EditorInfo.IME_ACTION_DONE)) {
processPayment();
return true;
}
else {
return false;
}
}
};
}
Any idea what can cause this?
I have four edittext (atm pin kind of thing) and I want to validate all these four edittext on click of keypads done button(validations like - empty or wrong pin). so far, I could get the validations but it happens only if I click the done buttn twice. not able to figure out the issue, please help. I am a beginner. thanks!
Below is my code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.pin_new);
setTextFocus(edtpin1, edtpin2);
setTextFocus(edtpin2, edtpin3);
setTextFocus(edtpin3, edtpin4);
setTextFocus(edtpin5, edtpin6);
setTextFocus(edtpin6, edtpin7);
setTextFocus(edtpin7, edtpin8);
edtpin1.setOnEditorActionListener(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView v, int arg1, KeyEvent arg2) {
switch (v.getId()) {
case R.id.btnBackPin:
Intent i = new Intent(getApplicationContext(),RegistrationProfileEmail.class);
startActivity(i);
break;
case R.id.btnCancelPin:
Intent in = new Intent(getApplicationContext(),Exit.class);
finish();
startActivity(in);
break;
case R.id.btnConfirmPin:
String s = new String();
s = edtpin1.getText().toString() + edtpin2.getText().toString()
+edtpin3.getText().toString() + edtpin4.getText().toString();
if ((edtpin5.getText().toString().trim().isEmpty())
|| (edtpin1.getText().toString().trim().isEmpty())
|| (edtpin2.getText().toString().trim().isEmpty())
|| (edtpin4.getText().toString().trim().isEmpty())
|| (edtpin3.getText().toString().trim().isEmpty())
|| (edtpin6.getText().toString().trim().isEmpty())
|| (edtpin7.getText().toString().trim().isEmpty())
|| (edtpin8.getText().toString().trim().isEmpty())) {
Toast.makeText(getApplicationContext(), "Enter the Pin",
Toast.LENGTH_SHORT).show();
edtpin1.setText(null);
edtpin2.setText(null);
edtpin3.setText(null);
edtpin4.setText(null);
edtpin5.setText(null);
edtpin6.setText(null);
edtpin7.setText(null);
edtpin8.setText(null);
edtpin1.requestFocus();
}
else if (edtpin1.getText().toString().trim().equalsIgnoreCase(edtpin5.getText().toString().trim())&& edtpin2.getText().toString().trim().equalsIgnoreCase(
edtpin6.getText().toString().trim()) && edtpin3.getText().toString().trim().equalsIgnoreCase( edtpin7.getText().toString().trim()) && edtpin4.getText().toString().trim().equalsIgnoreCase( edtpin8.getText().toString().trim()))
{
s = edtpin1.getText().toString() + edtpin2.getText().toString()
+ edtpin3.getText().toString()
+ edtpin4.getText().toString();
RegistrationPin.this.setPin(s);
}
// TODO move to payment option
else {
Toast.makeText(getApplicationContext(), "Pin is not matching",Toast.LENGTH_SHORT).show();
edtpin1.setText(null);
edtpin2.setText(null);
edtpin3.setText(null);
edtpin4.setText(null);
edtpin5.setText(null);
edtpin6.setText(null);
edtpin7.setText(null);
edtpin8.setText(null);
edtpin1.requestFocus();
}
break;
default:
break;
}
return false;
}
});
}
Create a class and implement the textwatcher like this:
private class CheckTextifEmpty implements TextWatcher {
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (isEmptyCharacters(serverNumber)
|| isEmptyCharacters(textview1)
|| isEmptyCharacters(textview2)
|| isEmptyCharacters(textview3)) {
//do something here
} else {
//do something here
}
}
}
and add this method here:
private boolean isEmptyCharacters(TextView v) {
return v.getText().toString().trim().isEmpty();
}
and use it in your textview like this:
textview1.addTextChangedListener(new CheckTextIfEmpty());
You can use your validation like this without using a button
Refactor your code like this:
private boolean checkifValidate(TextView view1,View TextView2){
return view1.getText().toString().trim().contentEquals(view2.getText().toString());
}
Hope it helps,
edtpin1.setOnKeyListener(onSoftKeyboardDonePress);
private View.OnKeyListener onSoftKeyboardDonePress=new View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
edtpin1.getText().toString();
edtpin2.getText().toString();
edtpin3.getText().toString();
edtpin4.getText().toString();
}
return false;
}
};
I want to add a search button on the keyboard input and the EditText and searches I proceeded as follows and I have them the following errors
recherche=(EditText)findViewById(R.id.recherche);
recherche.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
new DownloadTask().execute();
return true;
}
return false;
}
});
ERROR : The type new TextView.OnEditorActionListener(){} must implement the inherited abstract method
TextView.OnEditorActionListener.onEditorAction(TextView, int, KeyEvent)
ERROR : KeyEvent cannot be resolved to a type
Here is a sample of my code:
TextView.OnEditorActionListener enterListener = new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if ((keyEvent != null && (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (i == EditorInfo.IME_ACTION_DONE)) {
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
bQServer.performClick();
}
return false;
}
};
//etUserName.setOnEditorActionListener(enterListener);
etPassword.setOnEditorActionListener(enterListener);
You should be able to just change KeyEvent.KEYCODE_ENTER for KeyEvent.KEYCODE_SEARCH
i.e
TextView.OnEditorActionListener enterListener = new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if ((keyEvent != null && (keyEvent.getKeyCode() == KeyEvent.KEYCODE_SEARCH)) {
new DownloadTask().execute();
return true;
}
return false;
}
};
recherche.setOnEditorActionListener(enterListener);
--Edit--
Make sure you have imported KeyEvent and onEditorActionListener
import android.view.KeyEvent;
import android.widget.TextView.OnEditorActionListener;
I'm trying to catch the event of removing keyboard from the screen and i'm using OnEditorActionListener class. However, its onEditorAction method never gets called.
This is my EditText in XML:
<EditText
android:id="#+id/editTextSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:layout_weight="0.05"
android:background="#color/white"
android:ems="10">
</EditText>
This is how i work with it in java file:
private void createTextEdit()
{
EditText searchTextField = (EditText)findViewById(R.id.editTextSearch);
searchTextField.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
System.out.println("AFTER TEXT CHANGED");
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after)
{
System.out.println("BEFORE TEXT CHANGED " + s);
}
public void onTextChanged(CharSequence s,
int start, int before, int count)
{
System.out.println(s);
}
});
searchTextField.setOnEditorActionListener(new OnEditorActionListener()
{
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
System.out.println("ACTION ID " + actionId);//NEVER CALLED
if(actionId == EditorInfo.IME_ACTION_DONE)
{
System.out.println("ACTION DONE!!!!!!!!!!");
return true;
}
return false;
}
});
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(searchTextField, InputMethodManager.SHOW_IMPLICIT);
}
No matter what i do with searchTextField whether i start editing or finish it, the needed method is never fired. What am i doing wrong?
Same problem here on a Nexus 7.
I thought it was the imeOptions, but no.
It works in my tablet if i set android:inputType="text".
I assume that it works with other inputType.
I think it's a bug on Nexus.