开新活动的按钮?

时间:2011-09-04 16:19:29

标签: java android eclipse adt

所以,我有三个活动/屏幕。在第一个屏幕上,有一个按钮,按下该按钮可打开第二个屏幕。在第二个屏幕上,有一个用于打开屏幕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上的两个按钮同时工作?

1 个答案:

答案 0 :(得分: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>