2
我的要求是: - 如何在Android設置在動作條意見的「動作按鈕」
,當我點擊搜索按鈕,我想: - 因此,這裏是我的代碼: -
Snacks.java
public class Snacks extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_snacks);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33B5E5")));
getActionBar().setTitle("Snacks");
getActionBar().setIcon(R.drawable.ic_launcher);
getActionBar().setHomeButtonEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
searchView.setIconified(true);
int searchImgId = getResources().getIdentifier("android:id/search_button", null, null);
ImageView searchImg = (ImageView) searchView.findViewById(searchImgId);
searchImg.setImageResource(R.drawable.ic_action_search);
int searchDeleteImgId = getResources().getIdentifier("android:id/search_close_btn", null, null);
ImageView searchDeleteImg = (ImageView) searchView.findViewById(searchDeleteImgId);
searchDeleteImg.setImageResource(R.drawable.ic_action_delete);
int searchSrcTextId = getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchEditText = (EditText) searchView.findViewById(searchSrcTextId);
searchEditText.setTextColor(Color.WHITE);
searchEditText.setHintTextColor(Color.LTGRAY);
searchEditText.setHint("Search Here");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
Toast.makeText(getApplicationContext(), "Entered Keyword is " + s,Toast.LENGTH_LONG).show();
return true;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
return true;
}
}
activity_main_actions.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_search"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:showAsAction="always"
android:title="Search"/>
<item android:id="@+id/action_contact"
android:title="Contacts"
android:showAsAction="always"/>
<item android:id="@+id/settings"
android:title="Contacts"
android:showAsAction="never"/>
</menu>
styles.xml
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
但是當我運行這個程序,我得到這個: - 這裏,APPNAME 零食沒有顯示..當我點擊searchview我得到: -
此searchview擴展了全部。我的代碼中哪裏出錯我無法實現我的要求..