我在打字时使用MultiAutoCompleteTextView
来显示建议。我把MultiAutoCompleteTextView
放在AlertDialog
里。现在,它没有显示下拉。
我的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity=""
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/tv_query_statement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="@color/colorTextWhite"/>
<MultiAutoCompleteTextView
android:id="@+id/et_query"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:focusableInTouchMode="true"
android:padding="10dp"
android:textColor="@color/colorTextWhite"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:padding="10dp"
android:text="Submit"
android:textColor="@color/colorTextWhite"/>
</LinearLayout>
内部活动
private void setupQueryDialog() {
final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_query_dialog, null);
AppCompatButton btnsubmit = (AppCompatButton) view.findViewById(R.id.btn_submit);
final MultiAutoCompleteTextView mQueryEditor = (MultiAutoCompleteTextView) view.findViewById(R.id.et_query);
TextView mQueryStatement = (TextView) view.findViewById(R.id.tv_query_statement);
String[] commands = QueryHelper.getAllSqlCommands(mTableDetailSource);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, commands);
mQueryEditor.setThreshold(1);
mQueryEditor.setAdapter(adapter);
mQueryEditor.showDropDown();
mQueryEditor.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
/*if (editable.toString().length() > 0)
mQueryEditor.showDropDown();*/
}
});
dialog.setView(view);
final Dialog d = dialog.create();
btnsubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
submitQuery(mQueryEditor.getText().toString());
d.dismiss();
}
});
dialog.show();
}
发布于 2017-09-17 18:31:14
您可以更改您的实现。
当您想要显示下拉时,请考虑这一点,。通过详细的调查,我发现您的代码中存在一些问题。
尽管问题标题是这样写的,但我假设下拉列表实际上是可见的(如果没有像WindowManager$BadTokenException
这样的异常)。
但问题是,对话框出现在下拉菜单上,也就是说,下拉列表隐藏在对话框下面,并显示在您的活动中。
让我们检查一下.
您只需在mQueryEditor.setAdapter(adapter);
下面注释一行代码
String[] words = new String[]{
"ADD", "DELETE", "UPDATE", "DELETE FROM", "SELECT"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, words);
mQueryEditor.setThreshold(1);
mQueryEditor.setAdapter(adapter);
mQueryEditor.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
//mQueryEditor.showDropDown(); // Need to comment this line
并添加一个触摸listener
,它将帮助您显示下拉时,用户触摸它。
mQueryEditor.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mQueryEditor.showDropDown();
mQueryEditor.requestFocus();
return false;
}
});
注意:通过从activity
onCreate()
调用这个对话框可以生成WindowManager$BadTokenException
,然后应用程序就会崩溃。在所有关键的活动过程完成之后,尝试同样的方法。
https://stackoverflow.com/questions/46003242
复制相似问题