gpt4 book ai didi

android - 如何替换 MultiAutoCompleteTextView 下拉列表

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:52:40 26 4
gpt4 key购买 nike

我正在尝试用 ListView 替换 MultiAutoCompleteTextView 下拉列表,它应该具有与下拉列表相同的功能,这意味着,当我单击其中一个项目时,它应该被添加到 MultiAutoCompleteTextView 框中,等等,但会在您键入时过滤 ListView。

所以我想出了这个原始代码但没有成功:

filterable_listview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">

<MultiAutoCompleteTextView
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:hint="@string/To" android:id="@+id/search_box"></MultiAutoCompleteTextView>

<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"/>

</LinearLayout>

AutoCompleteActivity.java

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filterable_listview);
manager = new ContactManager();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, manager.getContacts());

searchEdit = (MultiAutoCompleteTextView) findViewById(R.id.search_box);

searchEdit.addTextChangedListener(filterTextWatcher);

searchEdit.setTokenizer(new SpaceTokenizer());

setListAdapter(adapter);

getListView().setOnItemClickListener(
new ListView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Here is one issues
searchEdit.append(adapter.getItem(position));
}
});
}

private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}

public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}

public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s);
}

};

我有主要问题:

1) 当我点击一个项目时,整个选定的文本被附加, 我知道问题在于:

searchEdit.append(adapter.getItem(position));

但是,我怎样才能像常规自动完成那样跨越文本呢?

2) 选择一个项目后,下一个输入不再显示建议(尽管有 SpaceTonekizer)

我希望我的解释很清楚。

提前致谢

最佳答案

如果有人需要做我需要的事情,我就做了这个

private MultiAutoCompleteTextView searchEdit;

private ArrayAdapter<String> adapter = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, manager
.getContacts());

setListAdapter(adapter);

searchEdit = (MultiAutoCompleteTextView) findViewById(R.id.search_box);

searchEdit.addTextChangedListener(filterTextWatcher);

searchEdit.setTokenizer(new SpaceTokenizer());

searchEdit.setAdapter(adapter);

searchEdit.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
String t = ((TextView) v).getText().toString();
String f = searchEdit.getText().toString();

int s = searchEdit.getSelectionStart();
int i = s;

while (i > 0 && f.charAt(i - 1) != ' ') {
i--;
}

adapter.getFilter().filter(t.substring(i, s));
}
});

okButton = (Button) findViewById(R.id.ok);
okButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Bundle stats = new Bundle();
stats.putString("ConversationName", searchEdit.getText()
.toString());

Intent i = new Intent();
i.putExtras(stats);
setResult(RESULT_OK, i);
finish();
}
});

searchEdit.setOnKeyListener(new OnKeyListener() {

public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT
|| keyCode == KeyEvent.KEYCODE_DPAD_RIGHT
|| keyCode == KeyEvent.KEYCODE_DPAD_UP
|| keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {

String t = ((TextView) v).getText().toString();
String f = searchEdit.getText().toString();

int s = searchEdit.getSelectionStart();
int i = s;

while (i > 0 && f.charAt(i - 1) != ' ') {
i--;
}

adapter.getFilter().filter(t.substring(i, s));

return false;
}

return false;
}
});

getListView().setOnItemClickListener(
new ListView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

String t = adapter.getItem(position);
String f = searchEdit.getText().toString();

int s = searchEdit.getSelectionStart();
int i = s;

while (i > 0 && f.charAt(i - 1) != ' ') {
i--;
}

searchEdit.getText().insert(s, t.substring(s - i));
}
});
}


@Override
protected void onDestroy() {
super.onDestroy();
searchEdit.removeTextChangedListener(filterTextWatcher);
}


private TextWatcher filterTextWatcher = new TextWatcher() {

public void afterTextChanged(Editable s) {
okButton.setEnabled(searchEdit.getText().toString().trim()
.length() > 0);
}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}

public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s);
}

};



public class SpaceTokenizer implements Tokenizer {

public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;

while (i > 0 && text.charAt(i - 1) != ' ') {
i--;
}
while (i < cursor && text.charAt(i) == ' ') {
i++;
}

return i;
}

public int findTokenEnd(CharSequence text, int cursor) {
int i = cursor;
int len = text.length();

while (i < len) {
if (text.charAt(i) == ' ') {
return i;
} else {
i++;
}
}

return len;
}

public CharSequence terminateToken(CharSequence text) {
int i = text.length();

while (i > 0 && text.charAt(i - 1) == ' ') {
i--;
}

if (i > 0 && text.charAt(i - 1) == ' ') {
return text;
} else {
if (text instanceof Spanned) {
SpannableString sp = new SpannableString(text + " ");
TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
Object.class, sp, 0);
return sp;
} else {
return text + " ";
}
}
}
}

xml 看起来像这样:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"
android:drawingCacheQuality="high">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">


<MultiAutoCompleteTextView
android:layout_marginLeft="1dip"
android:layout_marginTop="1dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="fill_parent"
android:dropDownHeight="0px"
android:hint="@string/To"
android:id="@+id/search_box"
></MultiAutoCompleteTextView>



<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="@+id/ok"
android:enabled="false"
/>


</LinearLayout>
<ListView android:id="@android:id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1" />

</LinearLayout>

希望对你有帮助

关于android - 如何替换 MultiAutoCompleteTextView 下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4209339/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com