java - 单击菜单后调用另一个 Activity

标签 java android android-layout android-studio

我有一个菜单,想要调用下一个 Activity ,以便用户可以计算然后想要返回下一页或主页,但不知道该怎么做...Ja尝试了很多方法,但不是正确的,或者当它在另一个页面上运行时,所有前面的菜单都会消失,只留下该 Activity 。

我是编程新手,尤其是 Android 编程新手。

主要 Activity :

@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            //I want to call the page " calculations" here.

        } else if (id == R.id.nav_gallery) {

    }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

android_manifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.stark.hello_world">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Splashscreen"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Calculos"
            android:label="@string/action_help"
            android:parentActivityName=".MainActivity" />

    </application>

</manifest>

calculos.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    tools:context=".MainActivity"
    android:background="#ff0fffab">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="SEM"
        android:id="@+id/textView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textSize="@dimen/abc_text_size_display_3_material"
        android:textColor="#ffffffff" />

    <EditText
        android:id="@+id/email_address"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />
</RelativeLayout>

app

最佳答案

我不确定您的问题,但我猜您想在单击菜单项后打开一个新的 Activity ?

为此,您需要在 if 中启动一个 Intent - 我已将您需要的行添加到代码中

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        //I want to call the page " calculations" here.
        //with this code you can call a new activity
        Intent intent = new Intent(this, YourActivity.class);
        startActivity(intent);
    } else if (id == R.id.nav_gallery) {
        //create another Intent here and navigate to the correct activity
}

但我建议使用 Fragments 而不是 Activity 并使用 getFragmentManager().beginTransaction().replace(YourFragment.newInstance());

关于java - 单击菜单后调用另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38038948/

相关文章:

java - 什么样的参数分配给 'HTTPServletResponse response'

android - Android中的SQLite查询来计算行数

android - 创建模糊透明背景效果

java - 服务器不响应时 JAX-WS SOAP 服务卡住

java - Spring-boot thymeleaf 从类路径加载 HTML 文件

android - AlarmManager 每 24 小时发出一次通知

android - 如何在 Camera2 API Android 中捕获叠加层内的图像?

java - 错误: android. widget.LinearLayout 无法转换为 android.widget.ImageView

android - EditText 的验证不起作用

java - 正确使用jar命令