简体   繁体   中英

Entering into text field (Android)

I have a editText in my view. Their is a else if statement connected to that editText. When you type in a certain word some code will run.

But when i type in the keyword the code isn't running. I think the problem is that their is a enter key. I just type the word in, if i hit enter afterwards it just creates a new line, rather then entering the text.
here is the code from my else if

public void editText (String editText)
{
    final ImageView image = (ImageView) findViewById(R.id.image1);
    final ImageView image1 = (ImageView) findViewById(R.id.imageView2);
    if(editText=="closer")
    {
        ((ViewGroup.MarginLayoutParams) image.getLayoutParams()).topMargin += 1;
//                image.setRotation(image.getRotation() + 1);
        image.requestLayout();
        ((ViewGroup.MarginLayoutParams) image1.getLayoutParams()).topMargin -= 1;
//                image2.setRotation(image2.getRotation() + 1);
    }

When comparing strings, you should use .equals instead of == , so it should be

if(editText.equals("closer"))

Also, as i can see in the comments, you are just calling the method on your onCreate, so it is being called once, when the activity is created. If you want to check it every time something is typed on the edit text, you should use a listener like this:

yourEditTextName.addTextChangedListener(new TextWatcher() {

  public void afterTextChanged(Editable s) {

    // this code will be called everytime the text changes
    editText(yourEditTextName.getText().toString());

  }

  public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

  public void onTextChanged(CharSequence s, int start, int before, int count) {}

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM