【问题标题】:How do I make the autocomplete prediction clickable?如何使自动完成预测可点击?
【发布时间】:2014-04-05 06:17:28
【问题描述】:

当我输入内容时,我会得到一个自动完成预测。就像当我输入“Joh”时,我得到“John F. Kennedy”。现在,如果用户点击“John F. Kennedy”,它会将他带入一个活动,该活动向他提供有关 John F. Kennedy 的信息。我应该怎么做才能得到这个?如何使自动完成预测可点击?

package com.mavenmaverick.autocomplete_test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;


public class MainActivity extends Activity {

String[] presidents= { "John F. Kennedy",
                    "Lyndon B. Johnson",
                    "Richard Nixon",
                    "Gerald Ford",
                    "Jimmy Carter",
                    "Ronald Reagan",
                    "George H. W. Bush",
                    "Bill Clinton",
                    "George W. Bush",
                    "Barack Obama"
                    };

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

    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, presidents);

    textView.setThreshold(3);
    textView.setAdapter(adapter);

}

}

【问题讨论】:

    标签: java android autocomplete clickable


    【解决方案1】:

    使用此代码。

    String[] presidents= { "John F. Kennedy",
                        "Lyndon B. Johnson",
                        "Richard Nixon",
                        "Gerald Ford",
                        "Jimmy Carter",
                        "Ronald Reagan",
                        "George H. W. Bush",
                        "Bill Clinton",
                        "George W. Bush",
                        "Barack Obama"
                        };
    private AutoCompleteTextView myAutoComplete;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
            myAutoComplete = (AutoCompleteTextView) findViewById(R.id.myautocomplete);
            myAutoComplete.addTextChangedListener(this);
            myAutoComplete.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, presidents));
    
    
    
    myAutoComplete.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(getBaseContext(), InforActivity.class);
                        intent.putExtra("president_name", obj.toString());
                        startActivity(intent);
                        finish();
    
                }
            });
    
    }
    

    【讨论】:

      【解决方案2】:

      你可以试试下面的代码,

      autoCompleteTextview.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
              //... your stuff
          }
      })
      

      您还有一个选择,当用户使用 setOnItemSelectedListener 实际选择下拉列表中的项目时设置点击侦听器。

      autoCompleteTextview.setOnSelectedListener(new OnItemClickListener() {
          @Override
          public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
              //... your stuff
          }
      });
      

      编辑

      if(position == 1){
      Intent i = new Intent (MainActivity.this, johncanedy.class);
      startActivity(i);
      }else if (position == 2){
      Intent i = new Intent (MainActivity.this, lyndon.class);
      startActivity(i);
      } 
      

      等等……

      【讨论】:

      • 我正在发布我的源代码。你能详细说明你的答案吗?
      • @RahulShaw,检查我的编辑,通过上面的代码获取位置,然后检查条件是否 pos 为 1,然后将意图传递到第二个屏幕,如果 pos 为 2,则再次新屏幕,就像这样。
      • 输入“Joh”时,我得到预测“John F. Kennedy”,但选择“John F. Kennedy”时没有任何反应。
      猜你喜欢
      • 2013-08-06
      • 1970-01-01
      • 2015-08-23
      • 2020-08-04
      • 2019-06-18
      • 2021-08-23
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      相关资源
      最近更新 更多