Android / Java:EditText(数字) - >如何为“完成”-Button创建onClick?

时间:2013-03-24 23:43:57

标签: java android android-edittext

我使用eclipse,我的Android-App需要一个解决方案。

在这个App中有一个EditText。其inputType设置为number。如果用户在此框中写入内容并按下数字键盘旁边的“完成”按钮,则会显示数字并退出键盘。但我的目标是使用这个“完成”按钮来完成一项简单的额外任务。通过单击此按钮,我希望将EditText中写入的数字保存在整数变量中。我知道如何通过检查或使用id来使用按钮,但我不知道(如何获得)这个特殊的“Done”-Button的ID。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

使用此代码

EditText editetext = (EditText) findViewById(R.id.editetext); // change this R.id.editetext to your EditText id
    Button doneButton = (Button) findViewById(R.id.doneButton); // change this R.id.doneButton to your Button id
            doneButton .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num = Integer.parseInt(editetext.getText.toString);
            }
        });

<强>更新

尝试此操作以在键盘中完成按钮操作

editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // do any thing here
        }
        return false;
    }
});