我有一个这样的对话框片段类:
public class dialogfrag extends DialogFragment
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater1 = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater1.inflate(R.layout.from, null))
// Add action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
}
在其布局 xml 文件 from.xml 中,它包含一个自动完成文本视图。这是 from.xml 文件的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="From"
android:gravity="center"
android:background="#FFFFBB33"
android:textAppearance="?android:attr/textAppearanceLarge" />
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1d"
android:layout_width="fill_parent"
android:layout_marginTop="15dp"
android:layout_height="wrap_content"
android:hint="From" >
<requestFocus />
</AutoCompleteTextView>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Place"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:weightSum="2" >
</LinearLayout>
我正在使用以下代码在单击按钮时弹出对话框片段。所以我给了这样的:
Button from=(Button)rootView.findViewById(R.id.button4);
from.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DialogFragment dialog=new dialogfrag();
dialog.show(getFragmentManager(), "fromtag");
}
});
现在我需要创建一个自动完成文本视图的对象,它应该向我显示建议。我有一个 arraylist,其中包含自动完成文本视图的 arrayadapter 的数据,我已经这样编码(这里 actv1 是自动完成文本视图的对象):
actv1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
String filter = s.toString().toLowerCase();
listItems = new ArrayList<String>();
for (String listItem : loc) {
if (listItem.toLowerCase().contains(filter))
{
listItems.add(listItem);
}
}
if (listItems.size() < 1){
Toast toast = Toast.makeText(getActivity().getApplicationContext(),
"No entries contain your search parameters",
Toast.LENGTH_SHORT);
toast.show();
}
ArrayAdapter<String> adapt=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItems);
actv1.setAdapter(adapt);
// actv1.setAdapter(new ArrayAdapter<String>(getActivity(),
// android.R.layout.simple_list_item_1, listItems
// .size()));
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
ArrayAdapter<String> adapt=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listItems);
actv1.setAdapter(adapt);
}
});
但问题是我不知道应该在哪里正确添加此代码,以便当我单击按钮时,会弹出一个对话框,其中包含 autocompletetextview。打字时,它应该使用我在这里提到的代码显示建议。有人可以帮我吗...