一箫一剑平生意,负尽狂名十五年。这篇文章主要讲述将Button链接到android中的现有活动相关的知识,希望能为你提供帮助。
我有一个已经为nav
抽屉装箱的活动。我想连接一个button
为search
快速navigation
,因为我不需要打开drawer
进行搜索这里是我的代码如下:
main.xml中
<
?xml version="1.0" encoding="utf-8"?>
<
menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<
item
android:id="@+id/nav_search"
android:icon="@drawable/ic_search"
android:title="@string/search"
app:showAsAction="always" />
<
/menu>
主要活动如下。
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {Fragment currFragment;
boolean isLoggedPrev, isLoggedCurrent;
NavigationView navigationView;
boolean doubleBackToExitPressedOnce = false;
public static int offsetY = 0;
private AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (UserAccessSession.getInstance(MainActivity.this).getUserSession() != null) {
isLoggedCurrent = true;
} else {
isLoggedCurrent = false;
}if (isLoggedCurrent != isLoggedPrev) {
isLoggedPrev = isLoggedCurrent;
checkLoginNavigation();
}
}
};
drawer.setDrawerListener(toggle);
toggle.syncState();
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
offsetY = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
Config.OFFSET_Y = offsetY;
}
showFragment(new HomeFragment());
showAds();
}public void checkLoginNavigation() {
navigationView.getMenu().clear();
UserSession userSession = UserAccessSession.getInstance(this).getUserSession();
if (userSession != null) {
navigationView.inflateMenu(R.menu.activity_main_drawer_logged);
} else {
navigationView.inflateMenu(R.menu.activity_main_drawer_not_logged);
}
}@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
doubleBackToExitPressedOnce = true;
Toast.makeText(this, R.string.tap_back_again_to_exit, Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
}@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu;
this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
Intent i;
int id = item.getItemId();
if(id == R.id.nav_home) {
showFragment(new HomeFragment());
setTitle(R.string.app_name);
}
if(id == R.id.nav_categories) {
showFragment(new CategoryFragment());
setTitle(R.string.categories);
}
if(id == R.id.nav_favorites) {
showFragment(new FavoritesFragment());
setTitle(R.string.favorites);
}
if(id == R.id.nav_featured) {
showFragment(new FeaturedFragment());
setTitle(R.string.featured);
}
if(id == R.id.nav_search) {
i = new Intent(this, SearchActivity.class);
startActivity(i);
}
if(id == R.id.nav_news) {
showFragment(new NewsFragment());
setTitle(R.string.latest_news);
}
if(id == R.id.nav_about_us) {
showFragment(new AboutUsFragment());
setTitle(R.string.about_us);
}
if(id == R.id.nav_terms_condition) {
showFragment(new TermsConditionFragment());
setTitle(R.string.terms_and_conditions);
}
if(id == R.id.nav_settings) {
i = new Intent(this, SettingsActivity.class);
startActivity(i);
}
if(id == R.id.nav_login) {
i = new Intent(this, LoginActivity.class);
startActivity(i);
}
if(id == R.id.nav_profile) {
i = new Intent(this, ProfileActivity.class);
startActivity(i);
}
if(id == R.id.nav_my_events) {
i = new Intent(this, MyEventsActivity.class);
startActivity(i);
}DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}public void showFragment(Fragment fragment) {
if (currFragment != null &
&
fragment.getClass().equals(currFragment.getClass()))
return;
currFragment = fragment;
FragmentManager fragmentManager = this.getSupportFragmentManager();
if (fragmentManager == null)
return;
FragmentTransaction ft = fragmentManager.beginTransaction();
if (ft == null)
return;
ft.replace(R.id.content_frame, fragment).commitAllowingStateLoss();
}
文章图片
我希望当我点击搜索按钮时,它会加载搜索活动,
navigation drawer
目前正在使用该搜索活动。答案【将Button链接到android中的现有活动】FYI
android:id="@+id/nav_search"
是MenuItem
不是NavigationItem
的一部分从你的onNavigationItemSelected()
删除它并添加到onOptionsItemSelected()
试试这个
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.nav_search) {
i = new Intent(this, SearchActivity.class);
startActivity(i);
return true;
}return super.onOptionsItemSelected(item);
}
另一答案它的MenuItem不是NavigationItem。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.nav_search) {
// do your stuff here
}
return super.onOptionsItemSelected(item);
}@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.yourMenu,menu);
return super.onCreateOptionsMenu(menu);
}
推荐阅读
- 如何检查Android应用程序是在后台还是前台或终止()
- 带有onClick项目的Android ListView
- Android应用开始和结束事件
- 将App发送回最近的应用程序堆栈android
- 全屏应用程序android
- 当用户关闭屏幕android时,使应用程序正常工作
- 我没有在我的Android回收站视图中看到mp3文件名,而是看到了他们的链接
- Android - Firebase addChildEventListener触发器[重复]
- Android微调器没有显示Firebase实时数据库数据()