进入文本字段(Android)

时间:2015-11-12 18:31:53

标签: android text field

我的视图中有一个editText。它们是连接到editText的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);
    }

1 个答案:

答案 0 :(得分:0)

比较字符串时,应使用.equals而不是==,因此它应该是

if(editText.equals("closer"))

另外,正如我在评论中看到的那样,您只是在onCreate上调用方法,因此在创建活动时会调用一次。 如果你想在每次在编辑文本上输入内容时检查它,你应该使用这样的监听器:

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) {}

});

相关问题