2011-09-04 94 views
1

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

+0

你怎么有两个名称相同的按钮?这是复制/粘贴错误吗? – Egor

+0

嗯,我猜我想我可以从按钮的代码工作,并复制它,所以另一个按钮将工作,但我想我忘了改变这一点。所以我现在改了它,但它仍然不起作用。 – Cole

回答

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 
     }  
     }); 

编辑:科尔,见下文,这是代码我的一个应用程序的工作,有两个不同的按钮指向两个不同的活动。我还包含了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> 
+0

当你说//代码在这里,我应该有两个相同的代码?我有:Intent myIntent = new Intent(view.getContext(),FirstActivity.class); startActivityForResult(myIntent,0);对于第一个按钮,但是当我在第二个按钮上也有时,它不起作用,但是第一个按钮可以。 – Cole

+0

如果你想加载不同的意图,那么你想要不同的代码。从我给你的代码开始,按照你所说的活动进行,你会希望得到** goto1 **:'Intent myIntent = new Intent(Screen2.this,FirstActivity.class); startActivityForResult(myIntent,0);'和这个为** goto3 **:'Intent myIntent = new Intent(Screen2.this,Screen3.class); startActivityForResult(myIntent,0);'。此外,如果这不起作用,请解释是否有任何错误显示,它在做什么,它不是什么等。 – Ricky

+0

我没有在Logcat中发现任何错误。当我点击goto1时,Logcat表示它显示活动Screen1(这是正确的),但goto3按钮根本没有做任何事情。 Logcat中没有任何内容,模拟器中没有任何操作。 – Cole