【问题标题】:how show recently entered text in edit text after entering as suggestion later on using shared preferences?如何在稍后使用共享首选项输入建议后在编辑文本中显示最近输入的文本?
【发布时间】:2018-08-05 15:04:16
【问题描述】:

我想要一个编辑文本,它显示最近输入的文本作为建议,当我在手机中输入不同的文本(如电话簿)时,如果前 3 个数字或字母匹配,则显示建议,如果不匹配,则不会显示建议.
谢谢。

        super.onCreate(savedInstanceState);
        setContentView(R.layout.onemore);
        srchButton = (Button) findViewById(R.id.search);
        editText_Acct_Id = (EditText) findViewById(R.id.acct_id);
        savednotes = getSharedPreferences("notes",MODE_PRIVATE);

        editText_Acct_Id.setText(savednotes.getString("tag", "Default Value"));

当我想将文本保存为建议时,这是我的点击

srchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(editText_Acct_Id.getText().length()>0){
                    makeTag(editText_Acct_Id.getText().toString());

                    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editText_Acct_Id.getWindowToken(),0);

                }

我调用的这个方法

   private void makeTag(String tag){
        String or = savednotes.getString(tag, null);
        SharedPreferences.Editor preferencesEditor = savednotes.edit();
        preferencesEditor.putString("tag",tag); //change this line to this
        preferencesEditor.commit();
    }

【问题讨论】:

  • 先向我们展示你的努力
  • 我已经尝试使用共享首选项,但我无法得到我想要的
  • 您可以将它们作为数组保存到文件或数据库中,并在需要时加载...
  • 我已经编辑了我的问题

标签: android sharedpreferences


【解决方案1】:

首先转换你的EditText->AutoCompleteTextView。 add 还将共享首选项数据存储到字符串数组列表中。 然后在使用下面的代码后...

        String[] countries = getResources().getStringArray(R.array.list_of_countries_name);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,countries);
    mAutoCompleteTextView.setAdapter(adapter);
    mAutoCompleteTextView.setThreshold(1);

【讨论】:

    猜你喜欢
    • 2021-11-07
    • 2021-12-06
    • 2016-03-24
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2015-07-04
    • 2021-05-14
    • 1970-01-01
    相关资源
    最近更新 更多