【问题标题】:How to implement the auto-complete word hint in Android?如何在 Android 中实现自动完成单词提示?
【发布时间】:2011-09-14 09:35:05
【问题描述】:

有没有办法在Android中实现自动完成单词提示,从数据库中检索单词提示?

【问题讨论】:

    标签: android autocomplete


    【解决方案1】:
    public static String[] COUNTRIES = { "Afganistan", "Algeria", "Albania",
            "Angola", "Anguilla", "Antigua and\nBarbuda", "Argentina",
            "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
            "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus" };
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
    
        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountry);
    
        countryNames = COUNTRIES; // this is where you would fire a loader
        // and set the adapter when you get a result, so the following would be in a callback
        ed.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countryNames));
    }
    

    【讨论】:

      【解决方案2】:

      它的自动完成文本视图

      main.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         >
      <TextView 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/hello"
         />
      <AutoCompleteTextView android:id="@+id/myautocomplete"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:completionThreshold="1"
      />
      </LinearLayout>
      

      AndroidAutoCompleteTextView.java

      package com.AndroidAutoCompleteTextView;
      
      import android.app.Activity;
      import android.os.Bundle;
      import android.text.Editable;
      import android.text.TextWatcher;
      import android.widget.ArrayAdapter;
      import android.widget.AutoCompleteTextView;
      
      public class AndroidAutoCompleteTextView extends Activity implements TextWatcher{
      
      AutoCompleteTextView myAutoComplete;
      String item[]={
        "January", "February", "March", "April",
        "May", "June", "July", "August",
        "September", "October", "November", "December"
      };
      
         /** Called when the activity is first created. */
         @Override
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
      
             myAutoComplete = (AutoCompleteTextView)findViewById(R.id.myautocomplete);
      
             myAutoComplete.addTextChangedListener(this);
             myAutoComplete.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, item));
      
         }
      
      @Override
      public void afterTextChanged(Editable arg0) {
       // TODO Auto-generated method stub
      
      }
      
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
       // TODO Auto-generated method stub
      
      }
      
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
       // TODO Auto-generated method stub
      
      }
      }
      

      【讨论】:

      • 太可惜了!我无法访问此链接,可能它被阻止了。
      猜你喜欢
      • 2014-12-20
      • 2017-06-16
      • 1970-01-01
      • 2021-06-28
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 2013-01-28
      相关资源
      最近更新 更多