繁体   English   中英

通过单击菜单按钮打开活动-Android Studio

[英]Open Activity by clicking in the menu button- Android Studio

我是一个初学者我有几天遇到这个问题

我没有找到解决方案。 我有一个菜单显示在活动中,当我单击时,我希望它打开一个新活动。

我的问题是,要在带有菜单的活动中放置什么,以及要在新活动中放置什么?

这是我的代码


Menu_chat.xml(我的菜单)

   android:id="@+id/salva_vida"
   android:icon="@drawable/salva_vida"
   android:title="@string/save_life"
   app:showAsAction="always" />

ChatActivity.java(这是带有菜单的活动)

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id) {
            case android.R.id.home:
                onBackPressed();
                return true;

            case R.id.salva_vida:
                ???????  (What put here?)------------------
                break;

tab2.java(这是新活动,我想打开它)

public class tab2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab2);
    }
}
Intent mIntent = new Intent(this, tab2.class);
startActivity(mIntent);

您的ChatActivity.java将如下所示:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    switch (id) {
        case android.R.id.home:
            onBackPressed();
            return true;

        case R.id.salva_vida:
            //Start Activity here
            Intent mIntent = new Intent(this, tab2.class);
            startActivity(mIntent);
            break;

Intent mIntent = new Intent(this,tab2.class); startActivity(mIntent);

@Override public boolean onOptionsItemSelected(MenuItem item){int id = item.getItemId(); 开关(id){case android.R.id.home:onBackPressed(); 返回true; case R.id.salva_vida://在此处开始活动Intent mIntent = new Intent(this,tab2.class); startActivity(mIntent); 打破;

不要忘了添加您的tab2活动来体现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM