The comment function is used in many projects. When users click comment, an input box will pop up for input, such as the following figure:
Here is the small demo:
demo address: https://github.com/midux001/InputDialog
The main page is a recycler view, which simulates some comment data. When the user clicks a comment, the dialog of the comment will pop up automatically, and at the same time, the dialog will be jacked up by the input method for easy input. The usage of RecyclerView is not mentioned. You can find many God's detailed introduction and use of it on the Internet.
Here are three points to note:
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 pop up at the bottom
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 initializing a dialog in activity, you need to set its OnKeyListener 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 key listener is the key event we need to monitor
DialogInterface.OnKeyListener keylistener = new DialogInterface.OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
Log.i("TAG", "keyboard code---" + keyCode);
if (keyCode == KeyEvent.KEYCODE_BACK) {
dialog.dismiss();
return false;
} else if(keyCode == KeyEvent.KEYCODE_DEL){//Delete key
return false;
}else{
return true;
}
}
};
As for the value of keyCode in Android, I have learned about it in the previous article. I 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
Here's a note: when testing in the project, I didn't judge the situation of keyCode == KeyEvent.KEYCODE_DEL. When pressing a key in the keyboard, EditText didn't respond. Only when I added the judgment can I have the corresponding backspace key. (the same goes for the Enter key.).