如何在Android Studio 3.6.2中为“设置”选项添加onclick监听器?

huangapple go评论89阅读模式
英文:

How to give onclick lisner to Settings option in android studio 3.6.2?

问题

我更新了我的Android Studio版本。在更新后的Android Studio版本中,如何为设置选项添加点击监听器。当我为相同的抽屉菜单项使用点击监听器时,抽屉菜单没有响应。

布局的屏幕截图

如何在Android Studio 3.6.2中为“设置”选项添加onclick监听器?

这是我的活动类的代码片段

public class HomeActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // 将每个菜单ID作为一组ID传递,因为每个菜单应被视为顶级目标。
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setDrawerLayout(drawer)
                .build();

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // 膨胀菜单;如果存在动作栏,则将项目添加到动作栏。
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}

但这不应影响抽屉导航中的其他菜单选项。

英文:

I updated my andoid studio version. In updated android studio version how to give onclick listener to settings option. when i used onclick listener to the same the drawer menu is not responding.

Screenshot of layout

如何在Android Studio 3.6.2中为“设置”选项添加onclick监听器?

This is my code snippet of activity class

    public class HomeActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}

But it should not effects to the other menu options in drawer navigation

答案1

得分: 0

根据Tie destinations to menu items documentation,您可以通过重写onOptionsItemSelected()来在菜单项上启动特定屏幕:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.onNavDestinationSelected(item, navController)
        || super.onOptionsItemSelected(item);
}

然后,请确保您的菜单项的android:id(例如,您截图中的action_settings)与导航图中指向用于设置的<fragment><activity>android:id匹配。

确保菜单项的android:id与导航图的android:id匹配的相同概念适用于所有菜单,包括导航抽屉中使用的菜单(这是默认模板的NavigationView开箱即用的方式)。

英文:

As per the Tie destinations to menu items documentation, you can launch a particular screen when your menu item by overriding onOptionsItemSelected():

@Override
public boolean onOptionsItemSelected(MenuItem item) {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.onNavDestinationSelected(item, navController)
|| super.onOptionsItemSelected(item);
}

Then make sure that the android:id of your menu item (for example, the action_settings in your screenshot) matches the android:id of a destination in your navigation graph XML pointing to the &lt;fragment&gt; or &lt;activity&gt; you're using for your settings.

This same concept of ensuring that your menu item android:id and navigation graph android:id match applies to all of the menus including those used in a navigation drawer (that's how the default template's NavigationView works out of the box).

答案2

得分: 0

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // 填充菜单;如果存在动作栏,则将项目添加到其中。
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // 项目 ID
        case R.id.logout:
            // 创建共享首选项
            SharedPreferences sp = getSharedPreferences(Constant.SHARED_PREF_NAME, Context.MODE_PRIVATE);
            
            // 创建编辑器以将值存储到共享首选项中
            SharedPreferences.Editor editor = sp.edit();
            
            // 向编辑器添加值
            editor.putString(Constant.ID_SHARED_PREF, "");
            
            // 将值保存到编辑器中
            editor.apply();
            
            Intent intent2 = new Intent(HomeActivity.this, UserLoginActivity.class);
            intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent2);
            
            return true;
        case R.id.settings:
            Intent intent = new Intent(HomeActivity.this, SettingsActivity.class);
            startActivity(intent);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}
英文:

You can try following source code in Activity

@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) {
switch (item.getItemId()) {
//item id
case R.id.logout:
//Creating a shared preference
SharedPreferences sp = getSharedPreferences(Constant.SHARED_PREF_NAME, Context.MODE_PRIVATE);
//Creating editor to store values to shared preferences
SharedPreferences.Editor editor = sp.edit();
//Adding values to editor
editor.putString(Constant.ID_SHARED_PREF, &quot;&quot;);
//Saving values to editor
editor.apply();
Intent intent2=new Intent(HomeActivity.this, UserLoginActivity.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent2);
return true;
case R.id.settings:
Intent intent=new Intent(HomeActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
default:
return super.onContextItemSelected(item);
}
}

huangapple
  • 本文由 发表于 2020年4月11日 13:18:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61152439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定