The comment function is used in many projects. When a user clicks a comment, an input box will pop up for input, such as the following figure:
Record this small demo here:
Demo address: https://github.com/midux001/InputDialog
The main page uses a RecyclerView to simulate some comment data. When the user clicks on a comment, the comment dialog will automatically pop up, and at the same time The dialog is jacked up by the input method for easy input. The usage of RecyclerView is not to mention, you can find a lot of great gods on the Internet to introduce and use it in detail.
There are three points to note here:
1. Custom dialog
public class CustomDialog extends Dialog {
EditText et_input;
public CustomDialog(@NonNull Context context) {
this(context, 0);
}
public CustomDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.module_dialog_layout);
setCanceledOnTouchOutside(true);
setCancelable(true);
Window window = getWindow();
window.setGravity(Gravity.BOTTOM);//dialog底部弹出
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(params);
et_input = findViewById(R.id.et_input);
}
}
- When the dialog is initialized in the activity, its OnKeyListener needs to be set, as follows:
adapter.setmOnItemClickListener(new CustommRecyclerViewAdapter.OnItemClickListener() {
@Override
public void onClick(View view, int position) {
mCustomDialog = new CustomDialog(MainActivity.this, R.style.customdialogstyle);
mCustomDialog.setOnKeyListener(keylistener);
mCustomDialog.show();
}
});
The keylistener is the key event we need to listen to
DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
Log.i("TAG", "键盘code---" + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK) {
dialog.dismiss();
return false;
} else if(keyCode == KeyEvent.KEYCODE_DEL){//删除键
return false;
}else{
return true;
}
}
};
Regarding the value of keyCode in Android, I have already learned about it in the previous article, and reprinted the blog of other friends. Interested friends can learn about it. The link is as follows: https://blog.csdn.net/midux/article/ details/80064054
There is a point to note here. When testing in the project, I did not judge the situation that keyCode == KeyEvent.KEYCODE_DEL. When pressing a key on the keyboard, EditText did not respond. Only after I added the judgment, I can backspace. corresponding to the key. (The same goes for the Enter key).