首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

求大神打救~两个按钮的跳转有关问题

2013-06-19 
求大神打救~!两个按钮的跳转问题!设置了两个按钮分别跳转到两个不同的页面,第三个按钮用于退出,模拟器运行

求大神打救~!两个按钮的跳转问题!
设置了两个按钮分别跳转到两个不同的页面,第三个按钮用于退出,模拟器运行后能弹出画面,但是当我点击main中的button1时页面直接跳转到了Activity03控制的main3页面(button5)按代码应该是点击button4才会跳到main3,点击button1时应该跳转到由Activity02控制的main2才对。所以麻烦各位大神看看以下的代码究竟错在哪里了。
src下分别是Activity01,Activity02,Activity03 对应的layout为 main,mian2,main3.代码如下


Activity01:

package com.lxt008;

import com.lxt008.R;

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

public class Activity01 extends Activity
{
private static final StringTAG= "Activity01";

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG, "onCreate");

Button button1 = (Button) findViewById(R.id.button1);
/* 监听button的事件信息 */
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(Activity01.this, Activity02.class);

/* 启动一个新的Activity */
startActivityForResult(intent, 1);
/* 关闭当前的Activity */
//Activity01.this.finish();
}
});


Button button4 = (Button) findViewById(R.id.button4);
/* 监听button的事件信息 */
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(Activity01.this, Activity03.class);

/* 启动一个新的Activity */
startActivityForResult(intent, 1);
/* 关闭当前的Activity */
//Activity01.this.finish();
}
});


/******************************/
Button button3 = (Button) findViewById(R.id.button3);
/* 监听button的事件信息 */
button3.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
/* 关闭当前的Activity */
Activity01.this.finish();
}
});
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.v("A1", ""+requestCode);
Log.v("A1", ""+resultCode);
Log.v("A1",data.getExtras().getString("result"));
}

public void onStart()
{
super.onStart();
Log.v(TAG, "onStart");
}

public void onResume()
{
super.onResume();
Log.v(TAG, "onResume");
}

public void onPause()
{
super.onPause();
Log.v(TAG, "onPause");
}

public void onStop()
{
super.onStop();
Log.v(TAG, "onStop");
}

public void onDestroy()
{
super.onDestroy();
Log.v(TAG, "onDestroy");
}

public void onRestart()
{
super.onRestart();
Log.v(TAG, "onReStart");
}

}

Activity02

:

package com.lxt008;

import com.lxt008.R;

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

public class Activity02 extends Activity
{
private static final StringTAG= "Activity02";

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Log.v(TAG, "onCreate");

Intent intent=new Intent();
intent.putExtra("result","success");
this.setResult(2,intent);

Button button = (Button) findViewById(R.id.button2);
/* 监听button的事件信息 */
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(Activity02.this, Activity01.class);
/* 启动一个新的Activity */

startActivity(intent);
/* 关闭当前的Activity */
Activity02.this.finish();
}
});
}

public void onStart()
{
super.onStart();

Log.v(TAG, "onStart");
}

public void onResume()
{
super.onResume();
Log.v(TAG, "onResume");
}

public void onPause()
{
super.onPause();
Log.v(TAG, "onPause");
}

public void onStop()
{
super.onStop();
Log.v(TAG, "onStop");
}

public void onDestroy()
{
super.onDestroy();
Log.v(TAG, "onDestroy");
}

public void onRestart()
{
super.onRestart();
Log.v(TAG, "onReStart");
}
}


Activity03:

package com.lxt008;

import com.lxt008.R;

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

public class Activity03 extends Activity
{
private static final StringTAG= "Activity03";

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Log.v(TAG, "onCreate");

Intent intent=new Intent();
intent.putExtra("result","success");
this.setResult(3,intent);

Button button = (Button) findViewById(R.id.button5);
/* 监听button的事件信息 */
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(Activity03.this, Activity01.class);
/* 启动一个新的Activity */

startActivity(intent);
/* 关闭当前的Activity */
Activity03.this.finish();
}
});
}

public void onStart()
{
super.onStart();

Log.v(TAG, "onStart");
}

public void onResume()
{
super.onResume();
Log.v(TAG, "onResume");


}

public void onPause()
{
super.onPause();
Log.v(TAG, "onPause");
}

public void onStop()
{
super.onStop();
Log.v(TAG, "onStop");
}

public void onDestroy()
{
super.onDestroy();
Log.v(TAG, "onDestroy");
}

public void onRestart()
{
super.onRestart();
Log.v(TAG, "onReStart");
}
}

main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    <Button
      android:id="@+id/button1"
      android:layout_width="100px"
      android:layout_height="wrap_content"
      android:layout_x="100px"
      android:layout_y="80px"
      android:text="Activity02"
    >
    </Button>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button4" />

    <Button
      android:id="@+id/button3"
      android:layout_width="100px"
      android:layout_height="wrap_content"
      android:layout_x="100px"
      android:layout_y="80px"
      android:text="Exit"
    >
    </Button>
</LinearLayout>

main2:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello2"
    />
    <Button
      android:id="@+id/button2"
      android:layout_width="100px"
      android:layout_height="wrap_content"
      android:layout_x="100px"
      android:layout_y="80px"
      android:text="Activity01"
    >
    </Button>
</LinearLayout>



main3:

<?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:orientation="vertical" >

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button5" />

</LinearLayout>

AndroidMainfest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.lxt008"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Activity01"
                  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="Activity02"></activity>
        <activity android:name="Activity03"></activity>
    </application>
    <uses-sdk android:minSdkVersion="7" />
</manifest> 


[解决办法]
Button button3 = (Button) findViewById(R.id.button3);
/* 监听button的事件信息 */
button3.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
/* 关闭当前的Activity */
Activity01.this.finish();
}
});

你看你button3加的什么事件,这有什么不明白了。

热点排行