我有一个RecyclerView Adapter类,其中每个视图持有者都有一个onclick侦听器。当用户单击视图持有人时,我希望显示一个带有自定义视图的对话框。
我的 dialog_custom.xml 是这样的:-
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
… >
<com.google.android.material.textfield.TextInputLayout
… >
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/txt_name"
… />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/btn_ok"
… />
</androidx.constraintlayout.widget.ConstraintLayout>
当用户单击Button( btn_ok )时,我想将在 txt_name 中输入的名称返回到ViewHolder类,并将其显示在TextView中。我应该做多少?
我的 MyAdapter.kt :-
class MyAdapter : RecyclerView.Adapter<MyAdapter.ViewHolder>() {
…
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
if (list.isEmpty()) holder.addMessage()
else holder.bind(list[position])
}
inner class ViewHolder(itemView: View, private val context: Context) : RecyclerView.ViewHolder(itemView) {
init {
itemView.setOnClickListener {
AlertDialog.Builder(context)
.setView(R.layout.dialog_custom)
.setCancelable(true)
.create().show()
}
}
…
}
}
答案 0 :(得分:0)
为此,您可以使用Kotlins MutableLiveData。使用包装器类显示AlertDialog并发布文本字段的值。
{gender: 'other'}
然后在您的Viewholder中调用:
class MyInputDialog(private val context){
public var input = MutableLiveData("")
public fun showDialog(){
val dialog = AlertDialog.Builder(context).run{
setView(R.layout.dialog_custom)
show()
}
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Confirm") { _, _ -> Unit
input.postValue(dialog.your_text_input.text.toString())
}
}
}
请注意,此代码尚未经过测试,但应该可以像这样工作。
答案 1 :(得分:0)
这是我要做的事情:
public class NumberDialog extends AppCompatDialogFragment {
private final static String DIALOG_TITLE_PARAM = "DIALOG_TITLE_PARAM";
private final static String DIALOG_PARAM_PARAM = "DIALOG_PARAM_PARAM";
private OnNumberDialogClickListener mCallback = null;
private EditText mEdit = null;
private int mParam = 0;
public interface OnNumberDialogClickListener {
void onNumberDialogPositiveClick(int req, int val);
}
public static NumberDialog newInstance(String title, int req) {
NumberDialog dialog = new NumberDialog();
Bundle params = new Bundle();
params.putString(DIALOG_TITLE_PARAM, title);
params.putInt(DIALOG_PARAM_PARAM, req);
dialog.setArguments(params);
return dialog;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
Fragment frag = getTargetFragment();
if (frag != null) {
mCallback = (OnNumberDialogClickListener)frag;
} else {
mCallback = (OnNumberDialogClickListener)context;
}
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
" must implement OnNumberDialogClickListener");
}
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString(DIALOG_TITLE_PARAM);
mParam = getArguments().getInt(DIALOG_PARAM_PARAM);
View layout = LayoutInflater.from(getContext()).inflate(R.layout.dialog_select_number, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(title).
setView(layout).
setPositiveButton(R.string.ok, null).
setNegativeButton(R.string.cancel, null);
mEdit = layout.findViewById(R.id.dialog_number_edit);
final AlertDialog alert_dialog = builder.create();
alert_dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
alert_dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mEdit != null) {
String valStr = mEdit.getText().toString();
int val;
try {
val = Integer.parseInt(valStr);
} catch (Exception e) {
return;
}
mCallback.onNumberDialogPositiveClick(mParam, val);
dismiss();
}
}
});
}
});
alert_dialog.setCanceledOnTouchOutside(true);
return alert_dialog;
}
}
这是数字对话框的示例类,该类验证输入为数字,并且仅在输入数字时才关闭警报对话框。如果数字有效,它将使用onNumberDialogPositiveClick()
回调到主类。
此代码向您展示如何:
只需创建NumberDialog
的实例并实现onNumberDialogPositiveClick()
来查看其工作原理。这为您提供了有关代码应如何更改的一般思路。