簡體   English   中英

在android studio中的第二個活動中打開第三個活動

[英]open third activity in second activity in android studio

我試圖寫我的應用程序第二頁,其中包含3個按鈕。 每個打開的新活動都是我的代碼,我的第二類xml和清單是我的代碼不起作用。

我的課

package com.mccam.pianopro;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class SecondPage extends Activity {
    private Button mButtonPlay;
    private MenuItem item;

    @Override
protected void onCreate(Bundle SecodState) {
    super.onCreate(SecodState);
    setContentView(R.layout.second);
    onOptionsItemSelected(item);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.piano:
        mButtonPlay = (Button)findViewById(R.id.piano);
        mButtonPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SecondPage.this, MainActivity.class));
                finish();
            }
        });

        case R.id.other:
            mButtonPlay = (Button)findViewById(R.id.other);
            mButtonPlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(SecondPage.this, OtherInd.class));
                    finish();
                }
            });

        case R.id.about_us:
            mButtonPlay = (Button)findViewById(R.id.about_us);
            mButtonPlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(SecondPage.this, AboutUs.class));
                    finish();
                }
            });
    }
    return true;
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}

}

我的xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/otherrrr"
android:weightSum="1">

    <LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_weight="0.22">

    <Button
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_weight="20"
    android:background="@drawable/piano1"
    android:id="@+id/piano" />

    <Button
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_weight="20"
    android:background="@drawable/other1"
    android:onClick=""
    android:id="@+id/other" />

    <Button
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_weight="20"
    android:background="@drawable/about1"
    android:id="@+id/about_us" />
    </LinearLayout>

我的清單

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mccam.pianopro"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="22" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.mccam.pianopro.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape" >

    </activity>

    <activity android:name="com.mccam.pianopro.SplashMain"
        android:label="@string/app_name"
        android:screenOrientation="landscape">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="com.mccam.pianopro.SecondPage"
        android:label="@string/SecondPage"
        android:screenOrientation="portrait">

    </activity>

    <activity android:name="com.mccam.pianopro.OtherInd"
        android:label="@string/Other"
        android:screenOrientation="landscape">

    </activity>

    <activity android:name="com.mccam.pianopro.AboutUs"
        android:label="@string/AboutUs"
        android:screenOrientation="landscape">

    </activity>

    <activity
        android:name="com.google.android.gms.ads.AdActivity"
                    android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|    screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />
</application>

</manifest>

您在這里混合了兩件事; onOptionsItemSelected用於處理菜單項的單擊,而不是按鈕的單擊。 刪除onOptionsItemSelected方法,並直接使用onCreate方法中的按鈕單擊邏輯。 像這樣:

    @Override
protected void onCreate(Bundle SecodState) {
    super.onCreate(SecodState);
    setContentView(R.layout.second);

    Button mButtonPlay1, mButtonPlay2, mButtonPlay3;

    mButtonPlay1 = (Button)findViewById(R.id.piano);
    mButtonPlay1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(SecondPage.this, MainActivity.class));
        }
    });

    mButtonPlay2 = (Button)findViewById(R.id.other);
    mButtonPlay2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(SecondPage.this, OtherInd.class));
        }
    });

    mButtonPlay3 = (Button)findViewById(R.id.about_us);
    mButtonPlay3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(SecondPage.this, AboutUs.class));
        }
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM