在android studio的第二个活动中打开第三个活动

时间:2015-08-08 06:44:49

标签: java android xml android-layout

我正在尝试编写包含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>

1 个答案:

答案 0 :(得分:0)

你在这里混合两件事; 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));
        }
    });
}