初学者 关于Activity 跳转

LZS535261548 2011-10-30 11:16:55
public class Test1Activity extends Activity {
/** Called when the activity is first created. */
private Button button2;
private Button button3;
private OnClickListener Listener;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
/*button2.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Test1Activity.this,test2.class);
startActivity(intent);

}});
button3.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Test1Activity.this,test3.class);
startActivity(intent);

}});*/
button2.setOnClickListener(Listener);
button3.setOnClickListener(Listener);
Listener = new OnClickListener()
{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button btn=(Button)v;
Intent intent = new Intent();
switch(btn.getId())
{
case R.id.button2:
intent.setClass(Test1Activity.this,test2.class);

startActivity(intent);
finish();
case R.id.button3:
intent.setClass(Test1Activity.this,test3.class);
startActivity(intent);
finish();


}

}
};}
}使用注释起来的可以 而使用switch就不可以了 不知道为什么这是
...全文
159 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
LZS535261548 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 peijiangping1989 的回复:]
楼主加油哇
[/Quote]

恩恩 努力进军安卓 这个比C++有意思 清闲的时候 学习安卓
小裴同学 2011-11-01
  • 打赏
  • 举报
回复

楼主加油哇
LZS535261548 2011-11-01
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 doveqian 的回复:]
LZ代码质量有待提升
[/Quote] 初学者 其他的慢慢培养
doveqian 2011-11-01
  • 打赏
  • 举报
回复
LZ代码质量有待提升
huanhuanfu 2011-10-31
  • 打赏
  • 举报
回复
break;
LZS535261548 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 june1991 的回复:]
Java code

package com.Test1Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;……
[/Quote]
代码没有问题 如果不适用switch()语句 就可以 但是代码就重复了
老林上猫扑 2011-10-31
  • 打赏
  • 举报
回复
是不是要加个break?
June1991 2011-10-31
  • 打赏
  • 举报
回复
package com.Test1Activity;

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

public class Test1Activity extends Activity implements OnClickListener
{
private Button button2;
private Button button3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
// button2.setOnClickListener(new OnClickListener()
// {
// public void onClick(View v)
// {
// // TODO Auto-generated method stub
// Intent intent = new Intent();
// intent.setClass(Test1Activity.this,test2.class);
// startActivity(intent);
// }
// });
// button3.setOnClickListener(new OnClickListener()
// {
// public void onClick(View v)
// {
// // TODO Auto-generated method stub
// Intent intent = new Intent();
// intent.setClass(Test1Activity.this,test3.class);
// startActivity(intent);
//
// }
// });
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
switch(v.getId())
{
case R.id.button2:
intent.setClass(Test1Activity.this,test2.class);
startActivity(intent);
finish();
break;
case R.id.button3:
intent.setClass(Test1Activity.this,test3.class);
startActivity(intent);
finish();
break;
}
}
}

你那样好像点击事件无法执行
LZS535261548 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 huanhuanfu 的回复:]
break;
[/Quote] 没有用啊
LZS535261548 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 huanhuanfu 的回复:]
break;
[/Quote]
没有用啊
fmworld 2011-10-30
  • 打赏
  • 举报
回复
这个不可以是什么意思啊,是报错了?如果是报错应该是你在switch里面没有加break断句,第一个case时你finish了,接着第二个case又调用这个activity对象估计就出错了。
LZS535261548 2011-10-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 waterstarsx 的回复:]
你那样写大概是没错的,不过你的switch 语句确实有问题
比如
#include<iostream>
#include<memory.h>
using namespace std;
int main()
{
int i = 0;
switch(i)
{
case 0:
cout <<"Hello Wrold1" << endl;

case 1:
cout <……
[/Quote] 这个是安卓的 C++的我懂
waterstarsx 2011-10-30
  • 打赏
  • 举报
回复
你那样写大概是没错的,不过你的switch 语句确实有问题
比如
#include<iostream>
#include<memory.h>
using namespace std;
int main()
{
int i = 0;
switch(i)
{
case 0:
cout <<"Hello Wrold1" << endl;

case 1:
cout <<"Hello Wolrd2" << endl;


}

return 0;

}

c++代码,输出结果是
Hello World1
Hello World2

#include<iostream>
#include<memory.h>
using namespace std;
int main()
{
int i = 0;
switch(i)
{
case 0:
cout <<"Hello Wrold" << endl;
break;
case 1:
cout <<"Hello Wolrd1" << endl;
break;
default:
break;
}

return 0;

}
这个结果才是
Hello World1也就是说如果没有break的话,下面的语句也会执行下去。startActivity(intent);并不是马上调转,而是向AmS申请跳转,后面的语句也会执行到
King_at_csdn 2011-10-30
  • 打赏
  • 举报
回复
没试过这种方式。直接
if(button2 == v){
} else if (button3 == v){
}
即可。
你把case语句里加个break;再试试。
android_newcomer 2011-10-30
  • 打赏
  • 举报
回复
感觉应该是switch的问题。。继续关注
LZS535261548 2011-10-30
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fmworld 的回复:]
这个不可以是什么意思啊,是报错了?如果是报错应该是你在switch里面没有加break断句,第一个case时你finish了,接着第二个case又调用这个activity对象估计就出错了。
[/Quote]
没有报错啊 就是活动没有办法跳转 想情况下是可以的 但是实际 上 仍是在一个界面的 就是没有跳转成功 如果活动个数就是两个的话 可相互跳转 三个 就不行 加上break .没有用 试过了

80,356

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧