【问题标题】:android, block special character in searchviewandroid,在searchview中阻止特殊字符
【发布时间】:2017-10-14 08:45:06
【问题描述】:

我是专门使用 android 编程的新手。 我在我的应用程序中使用 SearchView,但我不知道如何阻止特殊字符。 我已经看到有关 EditText 的主题,但没有看到有关 SearchView 的主题,我需要帮助。

我在应用栏中使用 SearchView 和这个 xml 作为菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_search_white_24dp"
        android:title="@string/search_title"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always" />
</menu>

还有这个活动代码:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);

}

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search_option_menu, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}

所以我等待你的回答,谢谢。

【问题讨论】:

  • 完成,这是来自 Google 的教程。

标签: android filter special-characters searchview


【解决方案1】:

您可以检测何时输入了字符:

//In onCreate
EditText et;//initialize
et.addTextChangedListener(tw);

onCreate 之外:

private TextWatcher tw = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //If the text contains a "bad" char, (char that isn't equal to A-Z/a-z or 0-9, remove it from the edittext
    }

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

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

(编辑文本应声明为类变量)

关于检测的建议:

//s is the char sequence

String string = s.toString();
boolean found = false;
StringBuilder sb = new StringBuilder();
for(int i = 0; i < s.length(); i++){
    String ch = s.charAt(i) + "";//Convert to check with regex
    if(!Pattern.compile("[A-Z|a-z|0-9]").matcher(ch).find()){
        found = true;
    }else{
        sb.append(ch);
    }

}
if(found){
    editText.setText(sb.toString());//only apply the text if a special char is found
}

我对特殊字符的解释是非数字和非 A-Z。可以对其进行修改以匹配您视为特殊的任何字符。如果您要禁止某些字符,最好检查这些字符,而不是检查它是否包含所有其他字符。

【讨论】:

  • 我不确定它是否适用于 SearchView,对于 SearchView 没有 setText()
【解决方案2】:

我遇到了将“>”转换为“|>”的问题。这就是我使用 Kotlin 修复它的方式:

override fun onQueryTextChange(newText: String?): Boolean {
    Log.w(TAG, "new text $newText")
    if (newText != null && newText.contains("|>")) {
        val text = newText?.replace("|>", ">")
        search.setQuery(text, false)
    } else {
          // do something
    }
    return false
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2023-04-08
    • 2018-03-26
    • 1970-01-01
    相关资源
    最近更新 更多