在EditText上的Android更改输入密钥以提交Butterknife

时间:2015-03-18 22:12:10

标签: android butterknife

我正在使用Butterknife并尝试在EditText上更改enter以进行提交

<EditText
android:id="@+id/id_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/gray_line_background"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
android:minLines="8" />

和我的片段

@InjectView(R.id.id_message)
@NotEmpty(messageResId = R.string.err_message_empty)
EditText message;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_myfragment, container, false);
      ButterKnife.inject(this, v);

      validator = new Validator(this);
      validator.setValidationListener(this);

      message.setImeActionLabel("Submit", KeyEvent.KEYCODE_ENTER);

      message.setOnEditorActionListener(new EditText.OnEditorActionListener() {
          @Override
          public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
              if (actionId == EditorInfo.IME_ACTION_DONE) {
                  onNextClick(message);
                  return true;
              }
              return false;
          }
      });


    return v;
}

按钮不会改变文本,并且监听器不执行操作的任何操作都不起作用

谢谢

1 个答案:

答案 0 :(得分:1)

问题不在于ButterKnife,问题出在xml

android:inputType="textMultiLine"

由于该行没有更改输入,因此我以编程方式管理

@InjectView(R.id.id_message)
@NotEmpty(messageResId = R.string.err_message_empty)
EditText message;

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_preclass3, container, false);
    ButterKnife.inject(this, v);

    message.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    message.setImeActionLabel("Enviar", EditorInfo.IME_ACTION_DONE);
    message.setImeOptions(EditorInfo.IME_ACTION_DONE);
    message.setLines(8);

    message.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                onNextClick(message);
                handled = true;
            }
            return handled;
        }
    });


    return v;
}

谢谢