【发布时间】:2014-01-21 06:37:51
【问题描述】:
我有一个自定义的 DialogFragment,其中有一个 ArrayAdapter,其中有一些 editTexts。当显示对话框时,即使我按下编辑文本,软键盘也不会出现。编辑文本确实获得焦点,但键盘从未出现。
如果我不使用适配器而只使用带有编辑文本的视图,它可以完美地工作,但是一旦我添加了适配器,我就会遇到问题。
我的代码如下。任何帮助将不胜感激。
提前致谢。
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;
}}
-布局
<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" />
-显示对话框的代码
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");
【问题讨论】:
-
是否需要使用适配器? - 见stackoverflow.com/questions/12876624/…
-
您好,很遗憾,是的。上面是一个简化的示例,演示了我遇到的问题。我的实际代码更复杂,适配器似乎是最好的方法。如果一切都失败了,我想我将不得不删除它。
标签: android android-arrayadapter android-dialogfragment