I am a Java rookie and need some help with my code.
I am trying to get a variable out of the code below so that I later on can make the numbers that you write in the EditText field display in as a TextView.
The code:
public class MoneyTickerActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText edittext = (EditText) findViewById(R.id.editText1);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(MoneyTickerActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
}
}
// get text view
final TextView texView = (TextView) findViewById(R.id.textView1);
and instead of
Toast.makeText(MoneyTickerActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();
make
textView.setText(edittext.getText());
Related
A beginner here, I am looking to make a simple app in which there are 4 sequential EditTexts, and as soon as the first EditText is completed and the user presses 'enter', the focus is automatically taken to the next EditText and the first EditText disappears.
I have used an 'onKey listener'. However, whenever the enter key is pressed for the first EditText, both the first and second EditText fields disappear.
Here is the code
public class MainActivity extends AppCompatActivity {
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText ko = findViewById(R.id.one);
final EditText ma = findViewById(R.id.two);
final EditText ko1 = findViewById(R.id.three);
final EditText xa = findViewById(R.id.four);
final TextView te = findViewById(R.id.ko);
ko.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(i == KeyEvent.KEYCODE_ENTER) {
ko.setVisibility(View.INVISIBLE);
ma.requestFocus();
}
return false;
}
});
ma.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent k) {
if(i == KeyEvent.KEYCODE_ENTER) {
ma.setVisibility(View.INVISIBLE);
ko1.requestFocus();
}
return false;
}
});
ko1.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent t) {
if(i == KeyEvent.KEYCODE_ENTER) {
ko1.setVisibility(View.INVISIBLE);
xa.requestFocus();
}
return false;
}
});
}
public void onClick(View v){
EditText ko = findViewById(R.id.one);
EditText ma = findViewById(R.id.two);
EditText ko1 = findViewById(R.id.three);
EditText xa = findViewById(R.id.four);
String t = ko.getText().toString();
String u = ma.getText().toString();
String x = ko1.getText().toString();
String w = xa.getText().toString();
String total = t +" "+ u +" "+x + " " +w;
Toast.makeText(this, total,Toast.LENGTH_LONG).show();
}
}
The event will continue to bubble up the container chain if you return false
Returns True if the listener has consumed the event, false otherwise.
ko.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(i == KeyEvent.KEYCODE_ENTER) {
ko.setVisibility(View.INVISIBLE);
ma.requestFocus();
}
return true;//change this line and all its occurances
}
});
Return true after requesting focus. Refer View.OnKeyListener
True if the listener has consumed the event, false otherwise.
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(i == KeyEvent.KEYCODE_ENTER) {
ko.setVisibility(View.INVISIBLE);
ma.requestFocus();
return true; // this will consume the Enter event
}
return false;
}
I'm trying to set the focus on an EditText (inputOne) but my code is showing odd behaviour. When I press the button, inputOne gets focus and everything is nice. If I press "Enter" on the SoftKeyboard, firing the onKeyListener, inputTwo remains focussed. In both cases "inputOne.requestFocus" returns true.
This is my Code:
public class EditSeriesActivity extends Activity {
private FlashcardSeries series;
private EditText inputOne;
private EditText inputTwo;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editseries);
Intent intent = getIntent();
series = intent.getParcelableExtra("EXTRA_MASSAGE");
TextView textView = (TextView) findViewById(R.id.nameseries);
textView.setText(series.getName());
inputOne = (EditText) findViewById(R.id.side1);
inputTwo = (EditText) findViewById(R.id.side2);
inputTwo.setOnKeyListener(new View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_DOWN)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
addCardToSeries(v);
break;
default:
break;
}
}
return false;
}
});
}
public void addCardToSeries(View view){
series.addFlashcard(new Flashcard(inputOne.getText().toString(), inputTwo.getText().toString()), this);
inputTwo.setText("");
inputOne.setText("");
inputOne.requestFocus();
}
}
try this
inputOne.requestFocus();
InputMethodManager inpuMethod = (InputMethodManager) getSystemService(G.context.INPUT_METHOD_SERVICE);
inpuMethod.showSoftInput(inputOne, InputMethodManager.SHOW_IMPLICIT);
I'm having a really hard time getting the OnKey event to trigger on my EditText view. I've been Googling for over an hour now and it seems everyone says all you have to do is add a OnKeyListener to the view but it's not triggering it. I tried adding it to the entire Activity and also tried adding it to the view itself but neither way is triggering the function.
My code:
XML:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/interests_editText" />
Java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_post);
interests_editText = (EditText) findViewById(R.id.interests_editText);
interests_editText.setOnKeyListener(new OnKeyListener(){
public boolean onKey( View view, int keyCode, KeyEvent event){
Log.i("DEBUG","TeST");
if (keyCode == KeyEvent.KEYCODE_ENTER) {
return true;
}
else {
return false;
}
}
}
);
Update:
My final goal is to get it to recognize when you press space while in that editText view. I added to the XML
android:inputType="text"
and then changed the code to
public boolean onKey( View view, int keyCode, KeyEvent event){
Log.i("DEBUG","Outside");
if (keyCode == KeyEvent.KEYCODE_SPACE) {
Log.i("DEBUG","Space");
return true;
}
else {
return false;
}
Now the keyboard shows a Next button that when pressed logs "Outside" but it doesn't log "Space"
Do you wanna capture Enter event? If so, try setOnEditorActionListener
For your Update Info: capture space key, try addTextChangedListener
Following up on #BNK's answer I got it to work using the following code:
interests_editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
String str = s.toString();
if(str.substring(str.length()-1, str.length()).equals(" "))
Log.i("DEBUG","Space pressed");
else if(str.substring(str.length()-1, str.length()).equals("\n"))
Log.i("DEBUG", "Enter pressed");
else
Log.i("DEBUG", "Pressed : " +str.substring(str.length()-1, str.length()) );
}}
I am making this try-out game and after someone guesses a number, the EditText view, needs to be cleared.
Here is my onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Button b = (Button) findViewById(R.id.check);
final EditText input = (EditText) findViewById(R.id.number);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!input.getText().toString().matches("")) {
input.setText("");
handleGuess(Integer.parseInt(input.getText().toString()));
} else {
Toast.makeText(getApplicationContext(), "Je hebt geen nummer ingevuld!", Toast.LENGTH_SHORT)
.show();
}
}
});
input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
if (!input.getText().toString().matches("")) {
input.setText("");
handleGuess(Integer.parseInt(input.getText().toString()));
} else {
Toast.makeText(getApplicationContext(), "Je hebt geen nummer ingevuld!", Toast.LENGTH_SHORT)
.show();
}
}
return false;
}
});
}
Whenever I try to press Enter on my softkeyboard or press the button, it just says "RaadHetGetal stopped."
Any ideas? If you need to see any more, just say it.
In your both listeners you have at least this error:
You set the text to nothing
input.setText("");
And try to parse an int from it directly after. This results in NumberFormatException
handleGuess(Integer.parseInt(input.getText().toString()));
So if you want to clear the text after handleGuess, you can call clear().
handleGuess(Integer.parseInt(input.getText().toString()));
input.clear();
Right, you obtain a java.lang.NumberFormatException, at line:
Integer.parseInt(input.getText().toString())
Since you are setting your TextView as empty with the code line:
input.setText("");
Can someone help me with a softkeyboard enter key listener?
I need a enter key listener like a button listener that would have a few editext listeners inside
like this
enterkey.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(editext1.getText().toString().equalsIgnoreCase("test1")) {
button3.performClick();
}
if(editext1.getText().toString().equalsIgnoreCase("test2")) {
button4.performClick();
}
}
);
I also need to know if something like this is correct?
if(editext1.getText().toString().equals.null)) {
testwrong.setText("Wrong");
I have now tried using this code but keep getting a null value when I hit enter?
Can anyone suggest a solution to avoid this?
editext.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER) {
if ("test1".equalsIgnoreCase(anstext.getText().toString())) {
but4.performClick();
}
} else if ("test2".equalsIgnoreCase(editext.getText().toString())) {
but5.performClick();
}
if ("test5".equalsIgnoreCase(editext.getText().toString())) {
but6.performClick();
}
if ("test7".equalsIgnoreCase(editext.getText().toString())) {
but7.performClick();
}
if (editext.getText().toString() != null) {
testwrong.seText("wrong");
}
return true;
}
});
In your EditText you should specify keyboard action using imeOptions.
<EditText
android:id="#+id/query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionGo"
android:inputType="text" />
And in your Activity's class:
EditText editText = (EditText) findViewById(R.id.query);
editText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
return true;
}
return false;
}
});
If you want to catch user press Enter register onKeyListener on your Edittext
yourEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode==KeyEvent.KEYCODE_ENTER) { //Whenever you got user click enter. Get text in edittext and check it equal test1. If it's true do your code in listenerevent of button3
if("test1".equals(edt.getText().toString())) {
//paste your code in button3 listener here
}
}
}
)
This part is wrong.
if(editext1.getText().toString().equals.null)) {
testwrong.setText("Wrong");
you should change to
if (editext1.getText().toString() != null && !editext1.getText().toString().isEmpty()) {
// doSomething
}
you can use this to listen to keyboard events
http://developer.android.com/reference/android/inputmethodservice/KeyboardView.OnKeyboardActionListener.html