Recyclerview与AlertDialog中的edittext无法正常工作

时间:2017-10-26 06:59:33

标签: android listview

您好我在alterbox中使用带有编辑文本的回收站视图 在edittext键盘中没有显示。
并尝试这个解决方案:

在mainfest XML中,我添加了这个

android:windowSoftInputMode="stateHidden|adjustPan"

在适配器类中:

editText.requestFocusFromTouch();

在recyclerview中,我试过这一行:

机器人:descendantFocusability = “beforeDescendants”

在上面我试过它不起作用:

我的代码:

Alter Box:

      final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view2 = inflater.inflate(R.layout.alter_box, null);
    alertDialogBuilder.setView(view2);
    String[] subjects =
            {
                    "ANDROID",
                    "PHP",
                    "BLOGGER",
                    "WORDPRESS",
                    "JOOMLA",
                    "ASP.NET",
                    "JAVA",
                    "C++",
                    "MATHS",
                    "HINDI",
                    "ENGLISH"};
    relativeLayout = (RelativeLayout) view2.findViewById(R.id.relativelayout1);
    recyclerView = (RecyclerView)view2. findViewById(R.id.recyclerview1);
    recylerViewLayoutManager = new LinearLayoutManager(MainActivity.this);
    recyclerView.setLayoutManager(recylerViewLayoutManager);
    recyclerViewAdapter = new RecyclerViewAdapter(context, subjects);
    recyclerView.setAdapter(recyclerViewAdapter);



    final AlertDialog dialog2 = alertDialogBuilder.create();
    alertDialogBuilder.setCancelable(true);
    dialog2.show();

RecyclerViewAdapter:

                  public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{

String[] SubjectValues;
Context context;
View view1;
ViewHolder viewHolder1;
TextView textView;

public RecyclerViewAdapter(Context context1,String[] SubjectValues1){

    SubjectValues = SubjectValues1;
    context = context1;
}

public static class ViewHolder extends RecyclerView.ViewHolder{
    public EditText textView;
    public ViewHolder(View v){
        super(v);
        textView = (EditText)v.findViewById(R.id.subject_textview);

    }
}

@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){

    view1 = LayoutInflater.from(context).inflate(R.layout.recyclerview_items,parent,false);

    viewHolder1 = new ViewHolder(view1);

    return viewHolder1;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position){

    holder.textView.setText(SubjectValues[position]);
    holder. textView.requestFocusFromTouch();
}

@Override
public int getItemCount(){

    return SubjectValues.length;
}
}

// 在alterbox中的recycleview上传递dailog不适合我。

ALTERDIALOG          最终AlertDialog.Builder alertDialogBu​​ilder = new AlertDialog.Builder(MainActivity.this);

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view2 = inflater.inflate(R.layout.alter_box, null);
    AlertDialog dialog = alertDialogBuilder.create();
    alertDialogBuilder.setView(view2);


    String[] subjects =
            {
                    "ANDROID",
                    "PHP",
                    "BLOGGER",
                    "WORDPRESS",
                    "JOOMLA",
                    "ASP.NET",
                    "JAVA",
                    "C++",
                    "MATHS",
                    "HINDI",
                    "ENGLISH"};
    relativeLayout = (RelativeLayout) view2.findViewById(R.id.relativelayout1);
    recyclerView = (RecyclerView)view2. findViewById(R.id.recyclerview1);
    recylerViewLayoutManager = new LinearLayoutManager(MainActivity.this);
    recyclerView.setLayoutManager(recylerViewLayoutManager);
    recyclerViewAdapter = new RecyclerViewAdapter(context, subjects,dialog);
    recyclerView.setAdapter(recyclerViewAdapter);
    final AlertDialog dialog2 = alertDialogBuilder.create();
    alertDialogBuilder.setCancelable(true);
    dialog2.show();

cONSTROUCTOR

TextView textView;     对话框对话框;

public RecyclerViewAdapter(Context context1, String[] SubjectValues1, Dialog dialog){

    SubjectValues = SubjectValues1;
    context = context1;
    dialogs=dialog;

} OnBindViewHolder

  holder.textView.setText(SubjectValues[position]);


    holder.textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                dialogs.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                dialogs.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        }
    });
}

2 个答案:

答案 0 :(得分:1)

onBindViewHolder 中添加此代码对我有用,参考this

将对话框传递给 RecyclerViewAdapter

alertDialogBuilder.setView(view2);
AlertDialog dialog = alertDialogBuilder.create();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new RecyclerViewAdapter(this, subjects, dialog));

使用对话框打开键盘。

holder.textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }
});

编辑解决方案代码

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view2 = inflater.inflate(R.layout.alter_box, null);
        String[] subjects = {"ANDROID", "PHP", "BLOGGER", "WORDPRESS", "JOOMLA", "ASP.NET"};
        RecyclerView recyclerView = view2.findViewById(R.id.rv);
        alertDialogBuilder.setView(view2);
        AlertDialog dialog = alertDialogBuilder.create();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new RecyclerViewAdapter(this, subjects, dialog));

        alertDialogBuilder.setCancelable(false);
        dialog.show();
    }

    public static class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

        private String[] subjects;
        private Context context;
        private AlertDialog dialog;

        RecyclerViewAdapter(Context context, String[] subjects, AlertDialog dialog) {

            this.subjects = subjects;
            this.context = context;
            this.dialog = dialog;
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            TextView textView;

            ViewHolder(View v) {
                super(v);
                textView = v.findViewById(R.id.et);
            }
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.recyclerview_items, parent, false));
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.textView.setText(subjects[position]);
            holder.textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return subjects.length;
        }
    }
}

答案 1 :(得分:1)

试试这个

  • 我认为您的edittext
  • 中没有使用任何customAlert
  • 分析您的alter_box.xml
  • 使用此layoutnamedas dialog_group_name.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:id="@+id/edt_groupName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:paddingLeft="@dimen/_10dp"
        android:paddingRight="@dimen/_10dp"
        android:textSize="@dimen/textSize"
        android:textColor="@color/colorPrimaryDark"
        android:textColorHint="@color/colorText"
        android:hint="@string/enter_groupName"
        android:inputType="textCapWords"
        android:singleLine="true"
        android:layout_marginTop="@dimen/_10dp"
        android:background="@null"
        android:maxLines="1"
        android:scrollbars="vertical" />
</LinearLayout>
  • 在此填充alert
public void inflateDialogue() {
        LayoutInflater layoutInflater=SelectUsers.this.getLayoutInflater();
        final View view = layoutInflater.inflate(R.layout.dialog_group_name, null);
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle(getResources().getString(R.string.Group_name));

        alertDialog.setCancelable(false);

        edt_groupName = (EditText) view.findViewById(R.id.edt_groupName);

        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                str_getTextFrom=edt_groupName.getText().toString();


                if(edt_groupName.getText().toString().trim().isEmpty()){
                    Toast.makeText(SelectUsers.this, "Empty Group Name", Toast.LENGTH_SHORT).show();
                }else {

                    }
                }

                dialog.dismiss();

            }
        });
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alertDialog.setView(view);
        alertDialog.show();
    }

很高兴为您提供帮助