2013-04-20 65 views
-1

我做了一个应用程序,有七个活动,都从主访问。每个都有自己的按钮,但是当我点击其中一个按钮,它正在开始所有的活动可以有人解决这个问题我/这是代码:Android应用活动导航

sat=(Button)findViewById(R.id.button7); 
    sun=(Button)findViewById(R.id.button1); 
    mon=(Button)findViewById(R.id.button2); 
    tues=(Button)findViewById(R.id.button3); 
    wed=(Button)findViewById(R.id.button4); 
    thurs=(Button)findViewById(R.id.button5); 
    fri=(Button)findViewById(R.id.button6); 
    info=(Button)findViewById(R.id.button8); 

    sat.setOnClickListener(this); 
    sun.setOnClickListener(this); 
    mon.setOnClickListener(this); 
    tues.setOnClickListener(this); 
    wed.setOnClickListener(this); 
    thurs.setOnClickListener(this); 
    fri.setOnClickListener(this); 
    info.setOnClickListener(this); 





    } 

public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    int id=arg0.getId(); 


    if(id==R.id.button6); 
    { 
     startActivity(new Intent(this,Fri.class)); 
    } 

    if(id==R.id.button5); 
    { 
     startActivity(new Intent(this,Thurs.class)); 
    } 

    if(id==R.id.button4); 
    { 
     startActivity(new Intent(this,Wed.class)); 
    } 

    if(id==R.id.button3); 
    { 
     startActivity(new Intent(this,Tues.class)); 
    } 

    if(id==R.id.button2); 
    { 
     startActivity(new Intent(this,Mon.class)); 
    } 

    if(id==R.id.button1); 
    { 
     startActivity(new Intent(this,Sun.class)); 
    } 

    if(id==R.id.button7); 
    { 
     startActivity(new Intent(this,Sat.class)); 
    } 
+0

只需使用开关的情况下..... – Pragnani 2013-04-20 11:37:41

回答

2

普莱舍语句试试下面的代码:解决您的问题

public void onClick(View arg0) 
{ 
    int id=arg0.getId(); 
    switch(id) 
    { 
    case R.id.button6: 
     startActivity(new Intent(this,Fri.class)); 
     break; 
    case R.id.button5: 
     startActivity(new Intent(this,Thurs.class)); 
     break; 
    case R.id.button4: 
     startActivity(new Intent(this,Wed.class)); 
     break; 
    case R.id.button3: 
     startActivity(new Intent(this,Tues.class)); 
     break; 
    case R.id.button2: 
     startActivity(new Intent(this,Mon.class)); 
     break; 
    case R.id.button1: 
     startActivity(new Intent(this,Sun.class)); 
     break; 
    case R.id.button7: 
     startActivity(new Intent(this,Sat.class)); 
     break; 
    default: 
     break; 
    } 
} 
+0

它的工作,但我改变了开关(ID)切换(arg0.getId()) – 2013-04-20 12:12:55

+0

好的。请考虑格式化您的代码。您可以在[Markdown帮助](http://stackoverflow.com/editing-help)中找到所有选项。 – 2013-04-20 12:14:48

1

使用else if而不是仅仅if

或使用switch caseonClick