編輯:關於Android編程
1.初始使用
android:completionThreshold="1"的作用是提示功能從第一個字母開始
package com.treasure_ct.study_demo.simplecontrols; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import com.treasure_ct.study_demo.R; import java.util.ArrayList; import java.util.List; /** * Created by treasure on 2016.08.27. */ public class AutoCompleteActivity extends Activity{ private AutoCompleteTextView autoCompleteTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simplecontrols_autocomplete); autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete); List效果list = new ArrayList<>(); list.add("String"); list.add("Integer"); list.add("Float"); list.add("Short"); list.add("Double"); ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,list); autoCompleteTextView.setAdapter(adapter); } }
2.實現可以使用拼音查找出對應的漢語
1)數據源格式 類
package com.treasure_ct.study_demo.simplecontrols; /** * Created by treasure on 2016.08.28. */ public class AutoEntry { private String text; private String pinyin; public AutoEntry(String text, String pinyin) { this.text = text; this.pinyin = pinyin; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getPinyin() { return pinyin; } public void setPinyin(String pinyin) { this.pinyin = pinyin; } @Override public String toString() { return text+"("+pinyin+ ")"; } }2)定義適配器 類
package com.treasure_ct.study_demo.simplecontrols; import android.content.Context; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Filter; import android.widget.Filterable; import android.widget.TextView; import java.util.ArrayList; import java.util.Collection; import java.util.List; /** * Created by treasure on 2016.08.28. */ public class AutoAdapter extends BaseAdapter implements Filterable{ private Context context; private Listlist; private Listbackup; private Filter filter; public AutoAdapter(Context context, List list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if (view == null){ view = new TextView(context); } ((TextView) view).setText(list.get(i).getText()); return view; } @Override public Filter getFilter() { if (filter == null){ filter = new Filter() { @Override protected FilterResults performFiltering(CharSequence charSequence) { if (backup == null){ backup = new ArrayList<>(list); } FilterResults results = new FilterResults(); if (!TextUtils.isEmpty(charSequence)) { List entries = new ArrayList<>(); for (AutoEntry entry : backup) { String pinyin = entry.getPinyin(); if (pinyin.contains(charSequence.toString())){ entries.add(entry); } } results.count = entries.size(); results.values = entries; }else { results.count = 0; results.values = new ArrayList<>(); } return results; } @Override protected void publishResults(CharSequence charSequence, FilterResults filterResults) { list.clear(); list.addAll((Collection)filterResults.values); notifyDataSetChanged(); } }; } return filter ; } }3)調用類
package com.treasure_ct.study_demo.simplecontrols; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import com.treasure_ct.study_demo.R; import java.util.ArrayList; import java.util.List; /** * Created by treasure on 2016.08.27. */ public class AutoCompleteActivity extends Activity{ private AutoCompleteTextView autoCompleteTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simplecontrols_autocomplete); autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete); // Listlist = new ArrayList<>(); List list = new ArrayList<>(); list.add(new AutoEntry("北京","beijing")); list.add(new AutoEntry("山東","shandong")); list.add(new AutoEntry("上海","shanghai")); list.add(new AutoEntry("廣東","guangdong")); list.add(new AutoEntry("浙江","zhejiang")); list.add(new AutoEntry("江蘇","jiangsu")); list.add(new AutoEntry("天津","tianjin")); list.add(new AutoEntry("遼寧","liaoning")); list.add(new AutoEntry("山西","shanxi")); list.add(new AutoEntry("河南","henan")); list.add(new AutoEntry("河北","hebei")); list.add(new AutoEntry("四川","sichuan")); AutoAdapter autoAdapter = new AutoAdapter(this,list); // ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,list); autoCompleteTextView.setAdapter(autoAdapter); } }
3.通過系統自帶的拼音jar包來通過拼音識別漢字(list只添加漢字,需要導入一個jpinyin的jar包)
自定義adapter類
package com.treasure_ct.study_demo.simplecontrols; import android.content.Context; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Filter; import android.widget.Filterable; import android.widget.TextView; import com.github.stuxuhai.jpinyin.PinyinException; import com.github.stuxuhai.jpinyin.PinyinFormat; import com.github.stuxuhai.jpinyin.PinyinHelper; import java.util.ArrayList; import java.util.Collection; import java.util.List; /** * Created by treasure on 2016.08.28. */ public class AutoStartWithAdapter extends BaseAdapter implements Filterable{ private Context context; private Listlist; private List backup; private Filter filter; public AutoStartWithAdapter(Context context, List list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { if (view == null){ view = new TextView(context); } ((TextView) view).setText(list.get(i)); return view; } @Override public Filter getFilter() { if (filter == null){ filter = new Filter() { @Override protected FilterResults performFiltering(CharSequence charSequence) { if (backup == null){ backup = new ArrayList<>(list); } FilterResults results = new FilterResults(); if (!TextUtils.isEmpty(charSequence)){ List strings = new ArrayList<>(); for (String str : backup) { try { String string = PinyinHelper.convertToPinyinString(str, "", PinyinFormat.WITHOUT_TONE); if (string.contains(charSequence)){ strings.add(str); }else { String shortPinyin = PinyinHelper.getShortPinyin(str); if (shortPinyin.startsWith(charSequence.toString())) { strings.add(str); } } } catch (PinyinException e) { e.printStackTrace(); } results.count = strings.size(); results.values = strings; } }else { results.count = 0; results.values = new ArrayList<>(); } return results; } @Override protected void publishResults(CharSequence charSequence, FilterResults filterResults) { list.clear(); list.addAll((Collection)filterResults.values); notifyDataSetChanged(); } }; } return filter; } }
主類
package com.treasure_ct.study_demo.simplecontrols; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import com.treasure_ct.study_demo.R; import java.util.ArrayList; import java.util.List; /** * Created by treasure on 2016.08.27. */ public class AutoCompleteActivity extends Activity{ private AutoCompleteTextView autoCompleteTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simplecontrols_autocomplete); autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete); // Listlist = new ArrayList<>(); // List list = new ArrayList<>(); // list.add(new AutoEntry("北京","beijing")); // list.add(new AutoEntry("山東","shandong")); // list.add(new AutoEntry("上海","shanghai")); // list.add(new AutoEntry("廣東","guangdong")); // list.add(new AutoEntry("浙江","zhejiang")); // list.add(new AutoEntry("江蘇","jiangsu")); // list.add(new AutoEntry("天津","tianjin")); // list.add(new AutoEntry("遼寧","liaoning")); // list.add(new AutoEntry("山西","shanxi")); // list.add(new AutoEntry("河南","henan")); // list.add(new AutoEntry("河北","hebei")); // list.add(new AutoEntry("四川","sichuan")); ArrayList list = new ArrayList<>(); list.add("北京"); list.add("上海"); list.add("山東"); list.add("廣東"); list.add("浙江"); list.add("江蘇"); list.add("天津"); list.add("遼寧"); list.add("山西"); list.add("河南"); list.add("河北"); list.add("四川"); // AutoAdapter autoAdapter = new AutoAdapter(this,list); AutoStartWithAdapter autoStartWithAdapter = new AutoStartWithAdapter(this,list); // ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,list); autoCompleteTextView.setAdapter(autoStartWithAdapter); } }
package com.treasure_ct.study_demo.simplecontrols; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.MultiAutoCompleteTextView; import com.treasure_ct.study_demo.R; import java.util.ArrayList; import java.util.List; /** * Created by treasure on 2016.08.27. */ public class AutoCompleteActivity extends Activity{ private AutoCompleteTextView autoCompleteTextView; private MultiAutoCompleteTextView multiAutoCompleteTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simplecontrols_autocomplete); autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoComplete); multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.multiautoComplete); // Listlist = new ArrayList<>(); // List list = new ArrayList<>(); // list.add(new AutoEntry("北京","beijing")); // list.add(new AutoEntry("山東","shandong")); // list.add(new AutoEntry("上海","shanghai")); // list.add(new AutoEntry("廣東","guangdong")); // list.add(new AutoEntry("浙江","zhejiang")); // list.add(new AutoEntry("江蘇","jiangsu")); // list.add(new AutoEntry("天津","tianjin")); // list.add(new AutoEntry("遼寧","liaoning")); // list.add(new AutoEntry("山西","shanxi")); // list.add(new AutoEntry("河南","henan")); // list.add(new AutoEntry("河北","hebei")); // list.add(new AutoEntry("四川","sichuan")); ArrayList list = new ArrayList<>(); list.add("北京"); list.add("上海"); list.add("山東"); list.add("廣東"); list.add("浙江"); list.add("江蘇"); list.add("天津"); list.add("遼寧"); list.add("山西"); list.add("河南"); list.add("河北"); list.add("四川"); // AutoAdapter autoAdapter = new AutoAdapter(this,list); AutoStartWithAdapter autoStartWithAdapter = new AutoStartWithAdapter(this,list); // ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,list); autoCompleteTextView.setAdapter(autoStartWithAdapter); multiAutoCompleteTextView.setAdapter(autoStartWithAdapter); MultiAutoCompleteTextView.CommaTokenizer commaTokenizer = new MultiAutoCompleteTextView.CommaTokenizer(); commaTokenizer.terminateToken(","); multiAutoCompleteTextView.setTokenizer(commaTokenizer); } }
搭建JAVA開發環境根據操作系統分為x86或x64位的,下載jdk1.8以上的版本,本機安裝時的java版本:jdk-8u45-windows-x64.exe配置JAVA
在Android中實現異步任務機制有兩種方式,Handler和AsyncTask。Handler模式需要為每一個任務創建一個新的線程,任務完成後通過Handler實例向U
Prime Land Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2972
使用樣式文件,在values 目錄下新建styles.xml文件,編寫如下代碼: 復制代碼 代碼如下: Code highlighting produced by Act