要在用户编辑完成后显示Toast,可以使用EditText的OnEditorActionListener接口来实现。
首先,在布局文件中定义一个EditText和一个Button用于用户输入和触发编辑完成的操作,例如:
接下来,在代码中找到EditText,并设置OnEditorActionListener,监听编辑完成的动作:
EditText editText = findViewById(R.id.editText);
Button button = findViewById(R.id.button);
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// 在这里显示Toast
Toast.makeText(MainActivity.this, "编辑完成", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 触发编辑完成的动作
editText.onEditorAction(EditorInfo.IME_ACTION_DONE);
}
});
在onEditorAction的回调方法中,可以在编辑完成时显示Toast。在这个例子中,我们使用了Toast.makeText来创建一个Toast,并调用show()方法显示出来。
另外,为了方便测试,我们还在Button的点击事件中调用了editText.onEditorAction(EditorInfo.IME_ACTION_DONE);来模拟用户编辑完成的操作。
这样,当用户点击软键盘的完成按钮或者点击Button时,都会显示一个Toast提示用户编辑完成。