因此,我有三个活动/屏幕。在第一个屏幕上,有一个按钮,按下该按钮可打开第二个屏幕。在第二个屏幕上,有一个按钮可以打开屏幕3,但还有另一个按钮可以返回屏幕1。我无法让屏幕2上的两个按钮都可以正常工作。 (一个有效,另一个无效,反之亦然)。

屏幕1 Java:

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

public class FirstActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button next = (Button) findViewById(R.id.part1to2);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Screen2.class);
            startActivityForResult(myIntent, 0);
        }

    });
   }
  }


屏幕2.java:

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

public class Screen2 extends Activity {

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    Button next = (Button) findViewById(R.id.goto1);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);


    Button next = (Button) findViewById(R.id.goto3);
    next.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
           Intent myIntent = new Intent(view.getContext(), Screen3.class);
            startActivityForResult(myIntent, 0);
            finish();
        }

      });
    }
  });
 }
}


屏幕3.java

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

public class Screen3 extends Activity {

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.third);

        }

}


而我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.gamer.network2"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".FirstActivity"
              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=".Screen2"></activity>
<activity android:name=".Screen3"></activity>
 </application>
 </manifest>


那么,如何使屏幕2上的两个按钮同时工作?

最佳答案

两个按钮的名称相同,并且声明错误。

  Button next = (Button) findViewById(R.id.goto1);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);


    Button next = (Button) findViewById(R.id.goto3);
    next.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
           Intent myIntent = new Intent(view.getContext(), Screen3.class);
            startActivityForResult(myIntent, 0);
            finish();
        }

      });
    }
  });


请尝试:

    Button goto3 = (Button) findViewById(R.id.goto3);
    goto3.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
           // Code here
       }
      });

    Button goto1 = (Button) findViewById(R.id.goto1);
    goto1.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
            // Code here
        }
      });


编辑:Cole,见下文,这是我的一个应用程序的代码,其中两个不同的按钮指向两个不同的活动。我还包括了AndroidManifest.xml中的代码。

    mBtnPlayCourse = (Button) findViewById(R.id.btn_screenstart_playcourse);
    mBtnNewCourse = (Button) findViewById(R.id.btn_screenstart_newcourse);

    mBtnPlayCourse.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent i = new Intent(Screen_Start.this, Screen_Players.class);
            startActivity(i);
        }
    });

    mBtnNewCourse.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent i = new Intent(Screen_Start.this, Screen_CreateCourse.class);
            startActivity(i);
        }
    });


AndroidManifest.xml中:

    <activity android:name=".Screen_CreateCourse" android:label="@string/app_name">
    </activity>

    <activity android:name=".Screen_Players" android:label="@string/app_name">
    </activity>

07-26 09:41