2014-09-25 132 views
-4

我想要制作一个小应用程序,其中MainActivity中有4个按钮,并且每个按钮都被编码为其特定类别,但是当我按下第一个按钮时,它会重新打开MainActivity而不是特定的页面,其余按钮不起作用。不按照代码工作的按钮

如何解决这个问题?

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Doubles = (Button) findViewById(R.id.button1); 
    Singles = (Button) findViewById(R.id.button2); 
    total = (Button) findViewById(R.id.button3); 
    score = (Button) findViewById(R.id.button4); 

    Doubles.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      Intent myintent1 = new Intent(MainActivity.this, Doubles.class); 

      startActivity(myintent1); 
     } 
    }); 

回答

0

问题是您的注册事件监听器只有双打按钮。 所以为了其他 做同样只使用

Singles.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      //stuff here 

     } 
    }); 

total.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      //stuff here 

     } 
    }); 

score.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      //stuff here 

     } 
    }); 

,并当您在双打..Your点击在Doubles.java其中U导航到名为Doubles.class第二页...... 检查setContentView()方法需要改变到另一个布局

0

你也可以用这种方式使用的onClick properity

//in activity 
public void doublesButtonClicked(View v) 
{ 
.... 
} 

//in xml 

<Button 
     android:id="@+id/button_double" 
     android:onClick="doublesButtonClicked" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:layout_marginLeft="55dp" 
     android:layout_marginTop="108dp" 
     android:layout_toRightOf="@+id/textView1" 
     android:text="Button" />