在 android 中第二次使用 onEditorActionListener 无效
Using onEditorActionListener doesn't work for the second time in android
我是 android 开发的新手,我想要实现的是:
当用户通过屏幕键盘(软输入法)点击完成按钮时,现有的默认 editText
应该变成 checkBox
并再次创建一个 editText
以便用户可以再次输入一些数据。
这些我应该可以通过创建一个按钮并按下它来做到这一点,但我不想要一个按钮,我想要屏幕上的键盘交互,比如 DONE 按钮。
首先,我用 onKeyListener 尝试过,但它在软键盘(移动 phone 的)上不起作用,而是在笔记本电脑键盘等硬件键盘上起作用。
然后我还可以使用 onEditerActionListener()
通过屏幕键盘执行上述功能,但只有一次我可以执行此操作,然后“完成”按钮从屏幕键盘,ENTER 按钮取代了 DONE 按钮。
编辑 每当我使用 editText.setSingleLine()
专注于 editText
时,总是会出现“完成”,但这次当我单击 editorActionListener
不起作用?
我不知道为什么完成按钮不像我第一次点击时那样工作
XML:
<EditText
android:id="@+id/defaultEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:inputType="text"
android:textSize="20sp"
android:imeOptions = "actionDone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
Android代码:
TextView.OnEditorActionListener doneButtonListener = ( new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//CREATING A CHECKBOX
CheckBox toDoCheckBox = new CheckBox(MainActivity.this);
toDoCheckBox.setText(defaultEditText.getText().toString());
toDoCheckBox.setTextSize(20);
toDoCheckBox.setId(View.generateViewId());
previousViewId = toDoCheckBox.getId();
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) defaultEditText.getLayoutParams();
ConstraintLayout.LayoutParams newParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
newParams.width = params.width;
newParams.height = params.height;
//Constraints
newParams.startToStart = params.startToStart;
newParams.endToEnd = params.endToEnd;
newParams.topToTop = params.topToTop;
newParams.topToBottom = params.topToBottom;
//Margins
newParams.leftMargin = params.leftMargin;
newParams.topMargin = params.topMargin;
newParams.rightMargin = params.rightMargin;
constraintLayout.addView(toDoCheckBox, -1, newParams);
defaultEditText.setVisibility(View.INVISIBLE);
// CREATING A EDITTEXT
EditText newEditText = new EditText(MainActivity.this);
newEditText.setWidth(defaultEditText.getWidth());
newEditText.setHeight(defaultEditText.getHeight());
newEditText.setId(View.generateViewId());
newEditText.setSingleLine();
ConstraintLayout.LayoutParams editTextParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
if (isFirst) {
editTextParams.topToBottom = previousViewId;
editTextParams.startToStart = previousViewId;
isFirst = false;
} else {
editTextParams.topToBottom = previousViewId;
editTextParams.startToStart = previousViewId;
}
defaultEditText = newEditText.findViewById(newEditText.getId());
newEditText.setLayoutParams(editTextParams);
constraintLayout.addView(newEditText);
return true;
}
return false;
}
});
defaultEditText.setOnEditorActionListener(doneButtonListener);
}
输出:
我重新发布了这个问题,因为我的问题没有得到答案
将以下属性添加到您的 edittext xml :
android:imeOptions="actionDone"
您正在 运行 创建新的编辑文本,您还需要为新的编辑文本初始化 editor action lister
。
更新后的代码应该是这样的
defaultEditText = findViewById(R.id.defaultEditText);
defaultEditText.setOnEditorActionListener(this);
编辑器动作
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
//CREATING A CHECKBOX
CheckBox toDoCheckBox = new CheckBox(TestingAcitvity.this);
toDoCheckBox.setText(defaultEditText.getText().toString());
toDoCheckBox.setTextSize(20);
toDoCheckBox.setId(View.generateViewId());
previousViewId = toDoCheckBox.getId();
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) defaultEditText.getLayoutParams();
ConstraintLayout.LayoutParams newParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
newParams.width = params.width;
newParams.height = params.height;
//Constraints
newParams.startToStart = params.startToStart;
newParams.endToEnd = params.endToEnd;
newParams.topToTop = params.topToTop;
newParams.topToBottom = params.topToBottom;
//Margins
newParams.leftMargin = params.leftMargin;
newParams.topMargin = params.topMargin;
newParams.rightMargin = params.rightMargin;
constraintLayout.addView(toDoCheckBox, -1, newParams);
defaultEditText.setVisibility(View.INVISIBLE);
// CREATING A EDITTEXT
EditText newEditText = new EditText(TestingAcitvity.this);
newEditText.setWidth(defaultEditText.getWidth());
newEditText.setHeight(defaultEditText.getHeight());
newEditText.setId(View.generateViewId());
newEditText.setSingleLine();
ConstraintLayout.LayoutParams editTextParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
if (isFirst) {
editTextParams.topToBottom = previousViewId;
editTextParams.startToStart = previousViewId;
isFirst = false;
} else {
editTextParams.topToBottom = previousViewId;
editTextParams.startToStart = previousViewId;
}
newEditText.requestFocus(); // request focus to focus on edit text
defaultEditText = newEditText.findViewById(newEditText.getId());
newEditText.setLayoutParams(editTextParams);
defaultEditText.setOnEditorActionListener(this); // set editor action listener
constraintLayout.addView(newEditText);
return true;
}
我是 android 开发的新手,我想要实现的是:
当用户通过屏幕键盘(软输入法)点击完成按钮时,现有的默认 editText
应该变成 checkBox
并再次创建一个 editText
以便用户可以再次输入一些数据。
这些我应该可以通过创建一个按钮并按下它来做到这一点,但我不想要一个按钮,我想要屏幕上的键盘交互,比如 DONE 按钮。
首先,我用 onKeyListener 尝试过,但它在软键盘(移动 phone 的)上不起作用,而是在笔记本电脑键盘等硬件键盘上起作用。
然后我还可以使用 onEditerActionListener()
通过屏幕键盘执行上述功能,但只有一次我可以执行此操作,然后“完成”按钮从屏幕键盘,ENTER 按钮取代了 DONE 按钮。
编辑 每当我使用 editText.setSingleLine()
专注于 editText
时,总是会出现“完成”,但这次当我单击 editorActionListener
不起作用?
我不知道为什么完成按钮不像我第一次点击时那样工作
XML:
<EditText
android:id="@+id/defaultEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:inputType="text"
android:textSize="20sp"
android:imeOptions = "actionDone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
Android代码:
TextView.OnEditorActionListener doneButtonListener = ( new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//CREATING A CHECKBOX
CheckBox toDoCheckBox = new CheckBox(MainActivity.this);
toDoCheckBox.setText(defaultEditText.getText().toString());
toDoCheckBox.setTextSize(20);
toDoCheckBox.setId(View.generateViewId());
previousViewId = toDoCheckBox.getId();
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) defaultEditText.getLayoutParams();
ConstraintLayout.LayoutParams newParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
newParams.width = params.width;
newParams.height = params.height;
//Constraints
newParams.startToStart = params.startToStart;
newParams.endToEnd = params.endToEnd;
newParams.topToTop = params.topToTop;
newParams.topToBottom = params.topToBottom;
//Margins
newParams.leftMargin = params.leftMargin;
newParams.topMargin = params.topMargin;
newParams.rightMargin = params.rightMargin;
constraintLayout.addView(toDoCheckBox, -1, newParams);
defaultEditText.setVisibility(View.INVISIBLE);
// CREATING A EDITTEXT
EditText newEditText = new EditText(MainActivity.this);
newEditText.setWidth(defaultEditText.getWidth());
newEditText.setHeight(defaultEditText.getHeight());
newEditText.setId(View.generateViewId());
newEditText.setSingleLine();
ConstraintLayout.LayoutParams editTextParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
if (isFirst) {
editTextParams.topToBottom = previousViewId;
editTextParams.startToStart = previousViewId;
isFirst = false;
} else {
editTextParams.topToBottom = previousViewId;
editTextParams.startToStart = previousViewId;
}
defaultEditText = newEditText.findViewById(newEditText.getId());
newEditText.setLayoutParams(editTextParams);
constraintLayout.addView(newEditText);
return true;
}
return false;
}
});
defaultEditText.setOnEditorActionListener(doneButtonListener);
}
输出:
我重新发布了这个问题,因为我的问题没有得到答案
将以下属性添加到您的 edittext xml :
android:imeOptions="actionDone"
您正在 运行 创建新的编辑文本,您还需要为新的编辑文本初始化 editor action lister
。
更新后的代码应该是这样的
defaultEditText = findViewById(R.id.defaultEditText);
defaultEditText.setOnEditorActionListener(this);
编辑器动作
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
//CREATING A CHECKBOX
CheckBox toDoCheckBox = new CheckBox(TestingAcitvity.this);
toDoCheckBox.setText(defaultEditText.getText().toString());
toDoCheckBox.setTextSize(20);
toDoCheckBox.setId(View.generateViewId());
previousViewId = toDoCheckBox.getId();
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) defaultEditText.getLayoutParams();
ConstraintLayout.LayoutParams newParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
newParams.width = params.width;
newParams.height = params.height;
//Constraints
newParams.startToStart = params.startToStart;
newParams.endToEnd = params.endToEnd;
newParams.topToTop = params.topToTop;
newParams.topToBottom = params.topToBottom;
//Margins
newParams.leftMargin = params.leftMargin;
newParams.topMargin = params.topMargin;
newParams.rightMargin = params.rightMargin;
constraintLayout.addView(toDoCheckBox, -1, newParams);
defaultEditText.setVisibility(View.INVISIBLE);
// CREATING A EDITTEXT
EditText newEditText = new EditText(TestingAcitvity.this);
newEditText.setWidth(defaultEditText.getWidth());
newEditText.setHeight(defaultEditText.getHeight());
newEditText.setId(View.generateViewId());
newEditText.setSingleLine();
ConstraintLayout.LayoutParams editTextParams = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
);
if (isFirst) {
editTextParams.topToBottom = previousViewId;
editTextParams.startToStart = previousViewId;
isFirst = false;
} else {
editTextParams.topToBottom = previousViewId;
editTextParams.startToStart = previousViewId;
}
newEditText.requestFocus(); // request focus to focus on edit text
defaultEditText = newEditText.findViewById(newEditText.getId());
newEditText.setLayoutParams(editTextParams);
defaultEditText.setOnEditorActionListener(this); // set editor action listener
constraintLayout.addView(newEditText);
return true;
}