gpt4 book ai didi

安卓 : Get the switch button action instead of selected navigation item

转载 作者:行者123 更新时间:2023-12-04 10:12:37 25 4
gpt4 key购买 nike

我有两个函数A,B,当用户改变给定开关按钮的值时,A或B会被执行。

我的问题是: Action A、B 仅在选择项目时执行,而不是在切换按钮的值更改时执行。

我试图从 onCreate 函数访问开关按钮,但我得到 NullPointerException。

我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">



<group android:checkableBehavior="single">
<item
android:id="@+id/switch_status"
android:title="Go offline"
android:checkable="true"
app:actionViewClass="android.widget.Switch"
/>
<item
android:id="@+id/nav_profile"
android:icon="@drawable/ic_action_profile"
android:title="My Profile" />
<item
android:id="@+id/nav_broadcast"
android:icon="@drawable/ic_action_broadcast"
android:title="My groups" />

<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_action_setting"
android:title="Settings" />
<item
android:id="@+id/nav_logout"
android:title="Logout" />
</group>

</menu>

我的 Activity 代码:

公共(public)类 MainActivity 扩展 AppCompatActivity
实现 NavigationView.OnNavigationItemSelectedListener、IUserClickListener{
    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utility.updateTheme(this);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(this, getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}

@Override
public void onPageSelected(int position) {
Fragment f = ((SectionsPagerAdapter) mViewPager.getAdapter()).getFragment(position);
if ((position == 0 || position == 1) && f != null) {
f.onResume();
}
}

@Override
public void onPageScrollStateChanged(int state) {
}
});
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
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) {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}

@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
updateMyInfo();
}
};
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setItemTextColor(new ColorStateList(state, color));
navigationView.setNavigationItemSelectedListener(this);

// Exception here

final Switch sw = (Switch) menu.findItem(R.id.switch_status);
sw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sw.isChecked()) {
Action A;
}
else {Action B;}
}

}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.switch_status) {
Switch sw = (Switch) findViewById(R.id.switch_status);
if (sw.isChecked()) {
Action A
} else {

Action B
}
}
}

单击切换按钮时如何更改我的代码以执行操作?

谢谢

最佳答案

onCreate的 Activity 被调用,选项菜单还没有膨胀。这就是为什么findViewById找不到您的 Switch .

将此添加到您的 Activity 中:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_drawer, menu);
final Switch sw = (Switch) findViewById(R.id.switch_status);
sw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
});
return true;
}
onCreateOptionsMenu被调用,当你的选项菜单被创建,你的菜单膨胀后,你可以访问 Switch照常。

关于安卓 : Get the switch button action instead of selected navigation item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61254071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com