【问题标题】:AutoCompleteTextview with acronym带有首字母缩写词的 AutoCompleteTextview
【发布时间】:2016-05-06 08:56:54
【问题描述】:

我是越南人,我使用我的语言为我的应用程序使用 Autocomplete TextView。我想制作 AutoComplete TextView,当我输入 AutoComplete TextView:“ban”时,它知道我想找到:“bạn”或“bán”或“bàn”。这意味着:我的语言中的“a”字符可以理解:á、ạ 或 à。我该怎么做。

public class Google extends Activity {

    AutoCompleteTextView edSearch;
    RelativeLayout.LayoutParams layoutParams;

    RelativeLayout rl;
    RelativeLayout rl1;
    ImageView img1;
    ImageView img2;
    ImageView img3;
    ImageView img4;
    ImageView img5;
    ImageView img6;
    ImageView logoGoogle;
    int status = 0;
    int keyboardstatus = 0;
    DatabaseHelper database;
    final ArrayList<String> array = new ArrayList<String>();



    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google);

        Intent returnIntent = new Intent();
        setResult(Activity.RESULT_OK, returnIntent);
        database = new DatabaseHelper(this);
       getListProduct();

        status = 0;
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);

        edSearch = (AutoCompleteTextView) findViewById(R.id.search);
        rl = (RelativeLayout) findViewById(R.id.mylayout);
        rl1 = (RelativeLayout) findViewById(R.id.mylayout1);

        final Intent intent = new Intent(this, SecondActivity.class);
        img1 = (ImageView) findViewById(R.id.img1);
        img2 = (ImageView) findViewById(R.id.img2);
        img3 = (ImageView) findViewById(R.id.img3);
        img4 = (ImageView) findViewById(R.id.img4);
        img5 = (ImageView) findViewById(R.id.img5);
        img6 = (ImageView) findViewById(R.id.img6);
        img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("message", "Ebay");
                startActivity(intent);
            }
        });
        img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("message", "Tiger");
                startActivity(intent);
            }
        });
        img3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("message", "Nice logo");
                startActivity(intent);
            }
        });
        img4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("message", "adidas");
                startActivity(intent);
            }
        });
        img5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("message", "b");
                startActivity(intent);
            }
        });
        img6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent.putExtra("message", "Nice");
                startActivity(intent);
            }
        });
        logoGoogle = (ImageView) findViewById(R.id.googlelogo);
        edSearch.setBackgroundResource(R.drawable.focus_edittext);
        edSearch.setAdapter(adapter);
        edSearch.setThreshold(1);

        edSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int index = array.indexOf(edSearch.getText().toString());
                // System.out.println(array.get(index) + "  : " + index); //check it now in Logcat

                intent.putExtra("message", array.get(index));
                startActivity(intent);
                edSearch.setText("");

            }
        });

        layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        final float d = this.getResources().getDisplayMetrics().density;
        int top = (int) (300 * d);
        int left = (int) (20 * d);
        int right = (int) (20 * d);
        layoutParams.setMargins(left, top, right, 0);
        edSearch.setLayoutParams(layoutParams);

        rl.setBackgroundResource(R.color.white);
        rl1.setBackgroundResource(R.color.white);

        edSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    performSearch();
                    return true;
                }
                if (event.getAction() == KeyEvent.ACTION_UP) {

                    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                        edSearch.setText("da back");
                    }

                }

                return false;
            }
        });





        //---------

        //-------
        edSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                status = 1;
                // Toast.makeText(getApplicationContext(), status + " khi da click ", Toast.LENGTH_SHORT).show();
                final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) edSearch.getLayoutParams();
                ValueAnimator animator = ValueAnimator.ofInt(params.topMargin, 0);
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        params.topMargin = (Integer) valueAnimator.getAnimatedValue();
                        edSearch.requestLayout();
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(edSearch.getWindowToken(), 0);
                    }
                });
                animator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        layoutParams.setMargins(0, 0, 0, 0);
                        edSearch.setLayoutParams(layoutParams);
                        rl.setBackgroundResource(R.color.dark);
                        getApplication().setTheme(R.style.BlackTheme);
                        img1.setColorFilter(R.color.dark);
                        img2.setColorFilter(R.color.dark);
                        img3.setColorFilter(R.color.dark);
                        img4.setColorFilter(R.color.dark);
                        img5.setColorFilter(R.color.dark);
                        img6.setColorFilter(R.color.dark);
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(edSearch, InputMethodManager.SHOW_IMPLICIT);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                animator.setDuration(200);
                animator.start();
            }
        });

【问题讨论】:

    标签: android autocompletetextview acronym


    【解决方案1】:

    您可以通过编写实现android.widget.Filterable 接口的自定义适配器来实现此目的。 您的自定义过滤器可以处理越南语中不同类型的 a。

    public class MyAdapter extends ArrayAdapter<String> {
    
       private Filter myFilter;
    
       public Filter getFilter() {
           if (myFilter == null) {
                myFilter= new MyArrayFilter();
           }
           return myFilter;
       }
    
       private class MyArrayFilter extends ArrayFilter {
           @Override
           protected FilterResults performFiltering(CharSequence prefix) {
              // Perform your filtering here. For example replace all possible a-versions with the basic a
              // Note that it's run in a background thread, so no UI interaction from here
          }
    
          @Override
          protected void publishResults(CharSequence constraint, FilterResults results) {
             // Gets called on the UI thread when filtering is done
             // Second param is return value from performFilter call
             // Put your results to the interface here
          }
       }
    }
    

    你可以在this stackoverflow answer找到一个例子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多