我们可以通过AutoCompleteTextView来让用户在输入时弹出建议项。
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.widget.ArrayAdapter;
importandroid.widget.AutoCompleteTextView;
importcom.sec.android.touchwiz.samples.R;
publicclassSearchBoxSample1XmlextendsActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.searchboxsample1xml_main);
ArrayAdapter<String>adapter=newArrayAdapter<String>(this,
R.layout.searchboxsample1xml_dropdown_text,COUNTRIES);
AutoCompleteTextViewtextView=(AutoCompleteTextView)findViewById(R.id.searchfield);
textView.setAdapter(adapter);
}
//用于在输入时弹出
privatestaticfinalString[]COUNTRIES=newString[]{
"Afghanistan","Albania","Algeria","AmericanSamoa","Andorra",
"Angola","Anguilla","Antarctica","AntiguaandBarbuda","Argentina",
"Armenia","Aruba","Australia","Austria","Azerbaijan",
"Bahrain","Bangladesh","Barbados","Belarus","Belgium",
"Belize","Benin","Bermuda","Bhutan","Bolivia",
"BosniaandHerzegovina","Botswana","BouvetIsland","Brazil","BritishIndianOceanTerritory",
"BritishVirginIslands","Brunei","Bulgaria","BurkinaFaso","Burundi",
"Coted'Ivoire","Cambodia","Cameroon","Canada","CapeVerde",
"CaymanIslands","CentralAfricanRepublic","Chad","Chile","China",
"ChristmasIsland","Cocos(Keeling)Islands","Colombia","Comoros","Congo",
"CookIslands","CostaRica","Croatia","Cuba","Cyprus","CzechRepublic",
"DemocraticRepublicoftheCongo","Denmark","Djibouti","Dominica","DominicanRepublic",
"EastTimor","Ecuador","Egypt","ElSalvador","EquatorialGuinea","Eritrea",
"Estonia","Ethiopia","FaeroeIslands","FalklandIslands","Fiji","Finland",
"FormerYugoslavRepublicofMacedonia","France","FrenchGuiana","FrenchPolynesia",
"FrenchSouthernTerritories","Gabon","Georgia","Germany","Ghana","Gibraltar",
"Greece","Greenland","Grenada","Guadeloupe","Guam","Guatemala","Guinea","Guinea-Bissau",
"Guyana","Haiti","HeardIslandandMcDonaldIslands","Honduras","HongKong","Hungary",
"Iceland","India","Indonesia","Iran","Iraq","Ireland","Israel","Italy","Jamaica",
"Japan","Jordan","Kazakhstan","Kenya","Kiribati","Kuwait","Kyrgyzstan","Laos",
"Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg",
"Macau","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","MarshallIslands",
"Martinique","Mauritania","Mauritius","Mayotte","Mexico","Micronesia","Moldova",
"Monaco","Mongolia","Montserrat","Morocco","Mozambique","Myanmar","Namibia",
"Nauru","Nepal","Netherlands","NetherlandsAntilles","NewCaledonia","NewZealand",
"Nicaragua","Niger","Nigeria","Niue","NorfolkIsland","NorthKorea","NorthernMarianas",
"Norway","Oman","Pakistan","Palau","Panama","PapuaNewGuinea","Paraguay","Peru",
"Philippines","PitcairnIslands","Poland","Portugal","PuertoRico","Qatar",
"Reunion","Romania","Russia","Rwanda","SqoTomeandPrincipe","SaintHelena",
"SaintKittsandNevis","SaintLucia","SaintPierreandMiquelon",
"SaintVincentandtheGrenadines","Samoa","SanMarino","SaudiArabia","Senegal",
"Seychelles","SierraLeone","Singapore","Slovakia","Slovenia","SolomonIslands",
"Somalia","SouthAfrica","SouthGeorgiaandtheSouthSandwichIslands","SouthKorea",
"Spain","SriLanka","Sudan","Suriname","SvalbardandJanMayen","Swaziland","Sweden",
"Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","TheBahamas",
"TheGambia","Togo","Tokelau","Tonga","TrinidadandTobago","Tunisia","Turkey",
"Turkmenistan","TurksandCaicosIslands","Tuvalu","VirginIslands","Uganda",
"Ukraine","UnitedArabEmirates","UnitedKingdom",
"UnitedStates","UnitedStatesMinorOutlyingIslands","Uruguay","Uzbekistan",
"Vanuatu","VaticanCity","Venezuela","Vietnam","WallisandFutuna","WesternSahara",
"Yemen","Yugoslavia","Zambia","Zimbabwe"
};
}
布局文件searchboxsample1xml_main.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android<wbr style="line-height:25px">"<br style="line-height:25px"> android:layout_width="fill_parent"<br style="line-height:25px"> android:layout_height="wrap_content"<br style="line-height:25px"> android:orientation="vertical"><br style="line-height:25px"><br style="line-height:25px"> <!--Header::searchbox--><br style="line-height:25px"> <LinearLayout<br style="line-height:25px"> android:layout_width="fill_parent"<br style="line-height:25px"> android:layout_height="58dp"<br style="line-height:25px"> android:orientation="horizontal"<br style="line-height:25px"> android:background="@drawable/searchboxsample1xml_header_bg"><br style="line-height:25px"> <</wbr>AutoCompleteTextView
android:id="@+id/searchfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:drawableLeft="@drawable/searchboxsample1xml_internet_google"
android:singleLine="true"
android:completionThreshold="1"
android:popupBackground="@drawable/searchboxsample1xml_dropdown_popup_bg"/>
<Button
android:id="@+id/searchbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/searchboxsample1xml_search"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<!--Items-->
<TextView
android:id="@+id/searchguidetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dp"
android:textColor="@touchwiz:color/tw_color001"
android:textSize="18sp"
android:text="@string/searchboxsample1xml_content"/>
</LinearLayout>
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.widget.ArrayAdapter;
importandroid.widget.AutoCompleteTextView;
importcom.sec.android.touchwiz.samples.R;
publicclassSearchBoxSample1XmlextendsActivity{
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.searchboxsample1xml_main);
ArrayAdapter<String>adapter=newArrayAdapter<String>(this,
R.layout.searchboxsample1xml_dropdown_text,COUNTRIES);
AutoCompleteTextViewtextView=(AutoCompleteTextView)findViewById(R.id.searchfield);
textView.setAdapter(adapter);
}
//用于在输入时弹出
privatestaticfinalString[]COUNTRIES=newString[]{
"Afghanistan","Albania","Algeria","AmericanSamoa","Andorra",
"Angola","Anguilla","Antarctica","AntiguaandBarbuda","Argentina",
"Armenia","Aruba","Australia","Austria","Azerbaijan",
"Bahrain","Bangladesh","Barbados","Belarus","Belgium",
"Belize","Benin","Bermuda","Bhutan","Bolivia",
"BosniaandHerzegovina","Botswana","BouvetIsland","Brazil","BritishIndianOceanTerritory",
"BritishVirginIslands","Brunei","Bulgaria","BurkinaFaso","Burundi",
"Coted'Ivoire","Cambodia","Cameroon","Canada","CapeVerde",
"CaymanIslands","CentralAfricanRepublic","Chad","Chile","China",
"ChristmasIsland","Cocos(Keeling)Islands","Colombia","Comoros","Congo",
"CookIslands","CostaRica","Croatia","Cuba","Cyprus","CzechRepublic",
"DemocraticRepublicoftheCongo","Denmark","Djibouti","Dominica","DominicanRepublic",
"EastTimor","Ecuador","Egypt","ElSalvador","EquatorialGuinea","Eritrea",
"Estonia","Ethiopia","FaeroeIslands","FalklandIslands","Fiji","Finland",
"FormerYugoslavRepublicofMacedonia","France","FrenchGuiana","FrenchPolynesia",
"FrenchSouthernTerritories","Gabon","Georgia","Germany","Ghana","Gibraltar",
"Greece","Greenland","Grenada","Guadeloupe","Guam","Guatemala","Guinea","Guinea-Bissau",
"Guyana","Haiti","HeardIslandandMcDonaldIslands","Honduras","HongKong","Hungary",
"Iceland","India","Indonesia","Iran","Iraq","Ireland","Israel","Italy","Jamaica",
"Japan","Jordan","Kazakhstan","Kenya","Kiribati","Kuwait","Kyrgyzstan","Laos",
"Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg",
"Macau","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","MarshallIslands",
"Martinique","Mauritania","Mauritius","Mayotte","Mexico","Micronesia","Moldova",
"Monaco","Mongolia","Montserrat","Morocco","Mozambique","Myanmar","Namibia",
"Nauru","Nepal","Netherlands","NetherlandsAntilles","NewCaledonia","NewZealand",
"Nicaragua","Niger","Nigeria","Niue","NorfolkIsland","NorthKorea","NorthernMarianas",
"Norway","Oman","Pakistan","Palau","Panama","PapuaNewGuinea","Paraguay","Peru",
"Philippines","PitcairnIslands","Poland","Portugal","PuertoRico","Qatar",
"Reunion","Romania","Russia","Rwanda","SqoTomeandPrincipe","SaintHelena",
"SaintKittsandNevis","SaintLucia","SaintPierreandMiquelon",
"SaintVincentandtheGrenadines","Samoa","SanMarino","SaudiArabia","Senegal",
"Seychelles","SierraLeone","Singapore","Slovakia","Slovenia","SolomonIslands",
"Somalia","SouthAfrica","SouthGeorgiaandtheSouthSandwichIslands","SouthKorea",
"Spain","SriLanka","Sudan","Suriname","SvalbardandJanMayen","Swaziland","Sweden",
"Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","TheBahamas",
"TheGambia","Togo","Tokelau","Tonga","TrinidadandTobago","Tunisia","Turkey",
"Turkmenistan","TurksandCaicosIslands","Tuvalu","VirginIslands","Uganda",
"Ukraine","UnitedArabEmirates","UnitedKingdom",
"UnitedStates","UnitedStatesMinorOutlyingIslands","Uruguay","Uzbekistan",
"Vanuatu","VaticanCity","Venezuela","Vietnam","WallisandFutuna","WesternSahara",
"Yemen","Yugoslavia","Zambia","Zimbabwe"
};
}
布局文件searchboxsample1xml_main.xml:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android<wbr style="line-height:25px">"<br style="line-height:25px"> android:layout_width="fill_parent"<br style="line-height:25px"> android:layout_height="wrap_content"<br style="line-height:25px"> android:orientation="vertical"><br style="line-height:25px"><br style="line-height:25px"> <!--Header::searchbox--><br style="line-height:25px"> <LinearLayout<br style="line-height:25px"> android:layout_width="fill_parent"<br style="line-height:25px"> android:layout_height="58dp"<br style="line-height:25px"> android:orientation="horizontal"<br style="line-height:25px"> android:background="@drawable/searchboxsample1xml_header_bg"><br style="line-height:25px"> <</wbr>AutoCompleteTextView
android:id="@+id/searchfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:drawableLeft="@drawable/searchboxsample1xml_internet_google"
android:singleLine="true"
android:completionThreshold="1"
android:popupBackground="@drawable/searchboxsample1xml_dropdown_popup_bg"/>
<Button
android:id="@+id/searchbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/searchboxsample1xml_search"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<!--Items-->
<TextView
android:id="@+id/searchguidetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dp"
android:textColor="@touchwiz:color/tw_color001"
android:textSize="18sp"
android:text="@string/searchboxsample1xml_content"/>
</LinearLayout>