I have a custom DialogFragment which has an ArrayAdapter in it which has some editTexts in it. When the dialog is shown the soft keyboard does not appear even if i press on the edit text. The edit text does take focus but the keyboard never comes up.
If i do not use an adapter and just use a view with an edit text it works perfectly but as soon as i add the adapter i get the issue.
My code is below. Any help would be really appreciated.
Thanks in Advance.
public class ParameterDialog extends DialogFragment {
Context context;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
ArrayList<Parameter> params = this.getArguments().getParcelableArrayList(MainActivity.KEY_PARAMS_TO_DIALOG);
String name = this.getArguments().getString(MainActivity.KEY_NAME_TO_DIALOG);
context = getActivity();
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
ParameterDialogAdapter pda = new ParameterDialogAdapter(context,R.layout.parameter_config_string , params);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder
.setTitle(name)
.setAdapter(pda,null)
.setPositiveButton("Send", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
// Create the AlertDialog object and return it
return builder.create();
}}
-
public class ParameterDialogAdapter extends ArrayAdapter<Parameter> {
Context context;
int layoutResourceId;
ArrayList<Parameter> data= null;
public ParameterDialogAdapter(Context context, int layoutResourceId, ArrayList<Parameter> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
//ParameterHolder holder = null;
final LayoutInflater inflater = ((Activity)context).getLayoutInflater();
final int p =position;
row = inflater.inflate(layoutResourceId, parent, false);
return row;
}}
-Layout
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/paramValueHolder"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingBottom="5dp">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/paramValue"
android:hint="Value"
android:textColor="@color/text_grey" />
-Code to Show Dialog
ParameterDialog pDialog = new ParameterDialog ();
Bundle bundle = new Bundle();
bundle.putString(KEY_NAME_TO_DIALOG,action.getName());
bundle.putParcelableArrayList(KEY_PARAMS_TO_DIALOG, params);
pDialog.setArguments(bundle);
pDialog.setArguments(bundle);
pDialog.show(ft, "parameter_dialog");
I have come across a similar problem while using a FragmentDialog
containing a ListView
. The items in the ListView
contained EditText
widgets that did not bring up the soft keyboard.
A workaround I discovered was to place an invisible EditText
within the root layout of the FragmentDialog
.
For example:
list_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@android:id/list"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
DialogFragment creates the views as follows:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
LayoutInflater inflater = getActivity().getLayoutInflater();
View listLayout = inflater.inflate(R.layout.list_layout, null);
mListView = (ListView) listLayout.findViewById(android.R.id.list);
return new AlertDialog.Builder(getActivity()).setView(listLayout).create();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
//set adapter
mListView.setAdapter(mListAdapter);
}
Hope this helps
set block descendants property true of your list_item_layout file.. i hope it helps
来源:https://stackoverflow.com/questions/21250931/soft-keyboard-doesnt-show-when-using-adapter-in-dialogfragment