- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试用 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/
我有一个像这样的数组 var resultsArray = [ { name: "BMW", value: "BMW", text: "BMW" }, { name: "Mercedes-Benz",
我正在尝试实现发现的下拉检查列表 here在 ASP.NET ListBox 控件上。它将控件呈现为下拉列表,并应用所有 css。但是,这些选项不是预期的复选框,而是单选按钮。有谁知道为什么当我让它与
如何使用 Javascript 在下拉列表的更改事件中获取先前选择的索引。 最佳答案 不,这是不可能的,但您可以在 onchange 事件中使用一个变量来跟踪之前的选择 示例: var previou
我想使用一个名为 dropdown-check-list 的插件: http://code.google.com/p/dropdown-check-list/ 但是,它的某些功能似乎与谷歌浏览器不兼容
我正在尝试找出如何制作类似于苹果商店的过滤选项的过滤选项。我首先想到的是类似于网站的下拉列表。但xcode中的对象选项上似乎没有它。想知道我应该从哪里开始才能实现这种功能。 和这个类似 http://
我正在尝试为类别创建一个下拉列表。如果这检查没问题,那么它必须是数据库。 型号: 分类 var $hasMany = 'Product'; 产品 var $belongsTo = 'Category'
有六个问题要问用户。如果用户回答了这些问题,我正在尝试制作一个应用程序,该应用程序将确定在右侧使用哪种研究设计的结果。我正在用 python dash 做这个应用程序。我的 Python 代码如下。如
我的问题是我所问问题的延续,请参阅链接。 Load Country/State/City 我已经展开以从数据库加载我的下拉列表,我只需要一种方法在我的第一个下拉列表和第二个下拉列表中连接 onchan
我正在尝试为一家餐厅创建一个内部成本核算电子表格。我重新熟悉了如何创建下拉列表(在本例中用于选择成分)。 当有人选择例如从下拉列表中选择“胡萝卜”,我希望其他字段能够使用另一个电子表格中的成本数据自动
JavaScript/jQuery 新手。我在有序列表中显示了一些数据,如下所示 Data 0 Data 1. Da
我在其中一个主题上发现了这一点: http://jsfiddle.net/GHzfD/357/我想问一下从下拉列表中选择图像后如何提醒(路径)。 $("body select").msDropDow
我使用 JAVA Swing 创建了一个下拉列表。当我选择“跟踪 RCM 的状态:”时,我想在所选选项旁边创建另一个下拉列表。我应该使用 mouseactionlistener 代替吗?我试图完成类似
在 Symfony2 网站中,我尝试制作一个包含 2 个(或 3 个)下拉列表的表单,其依赖关系为国家 > 地区 > 城市。该城市是我正在使用表单编辑的元素的字段。这个想法是根据选择来填充列表。 我已
我正在尝试创建一个菜单来计算 的数量如果列表中的数量超过 5 个,请将其余的移动到下拉列表中。 基本上代码如下所示: Item 1 Item 2 Item 3 Item 4 I
当我点击要安装主题的部门时,当我点击主题时要安装的服务。但当我点击服务时却没有看到问题。我认为对json的描述不准确。你能帮我解决这个问题吗?谢谢。我的 Jquery 代码; /* Select';
我有一个包含两个值的下拉列表:Sponsor 和 Social_Worker。我想要做的是,当选择其中一个时,它会显示一个 div 并隐藏另一个 div,如果选择另一个则反之亦然。我设法使用按钮执行此
我正在创建 2 个下拉列表,第二个下拉列表基于对第一个下拉列表的选择。从mysql数据库中获取数据 索引.php P
我正在尝试使用 JS 创建互斥的下拉菜单。 只能从这 4 个操作系统中选择一个:image 当一个被选中时,其他的应该被禁用。 HTML 部分: Re
首先,我是 java 脚本的新手。我正在开发我的 Web 应用程序,我有一个下拉菜单,其中包含人员列表。使用它我知道如何传递一个人的选定值。但是我如何选择多个值(人名)并将该数据发送到后端实现。是否可
我正在使用 Laravel 框架,并且有两个下拉列表,它们都从数据库表中读取数据, 第一个它从表中读取所有记录并将其填充到选择列表中 这是我的代码: {{Form::select
我是一名优秀的程序员,十分优秀!