我目前是一名高中生,为了好玩而决定尝试使用 Android 开发,但我很难过。我有一个蓝队和红队的图像按钮。蓝队的分数自动上升。我不知道该怎么做,当你按下红色按钮时,图像按钮会使红队得分上升,反之亦然。
这是我的java代码包com.example.puremmacompetitionjiujitsuscorer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
class MainActivity extends Activity {
private int blueScore = 0;
private int redScore = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageButton sweep = (ImageButton) findViewById(R.id.imageButton5);
final ImageButton pass = (ImageButton) findViewById(R.id.imageButton8);
final ImageButton mount = (ImageButton) findViewById(R.id.imageButton4);
final ImageButton backMount = (ImageButton) findViewById(R.id.imageButton3);
final ImageButton kneeOnBelly = (ImageButton) findViewById(R.id.imageButton7);
final ImageButton takeDown = (ImageButton) findViewById(R.id.imageButton6);
final TextView blueScoreCount = (TextView) findViewById(R.id.blueScore1);
takeDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
blueScore += 2;
blueScoreCount.setText("" + blueScore);
}
});
sweep.setOnClickListener(new View.OnClickListener(){
public void onClick(View w) {
blueScore += 2;
blueScoreCount.setText("" + blueScore);
}
});
pass.setOnClickListener(new View.OnClickListener(){
public void onClick(View q){
blueScore += 3;
blueScoreCount.setText("" + blueScore);
}
});
mount.setOnClickListener(new View.OnClickListener(){
public void onClick(View t){
blueScore += 4;
blueScoreCount.setText("" + blueScore);
}
});
backMount.setOnClickListener(new View.OnClickListener(){
public void onClick(View s){
blueScore += 4;
blueScoreCount.setText("" + blueScore);
}
});
kneeOnBelly.setOnClickListener(new View.OnClickListener(){
public void onClick(View g){
blueScore += 2;
blueScoreCount.setText("" + blueScore);
}
});
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
这是我的 XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/purebig" >
<TextView
android:id="@+id/redScore1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/hint"
android:maxLength="2"
android:textIsSelectable="false" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/stop" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="@drawable/play"/>
<ImageButton
android:id="@+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageButton2"
android:layout_alignParentLeft="true"
android:src="@drawable/kneeonbelly"
android:onClick="addTwo" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageButton7"
android:src="@drawable/backmount"
android:onClick="addFour" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageButton3"
android:layout_alignParentRight="true"
android:src="@drawable/mount"
android:onClick="addFour" />
<ImageButton
android:id="@+id/imageButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageButton7"
android:layout_centerHorizontal="true"
android:src="@drawable/pass"
android:onClick="addThree" />
<ImageButton
android:id="@+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/imageButton8"
android:src="@drawable/takedown"
android:onClick="addTwo" />
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton8"
android:layout_below="@+id/imageButton8"
android:src="@drawable/sweep"
android:onClick="addTwo" />
<ImageButton
android:id="@+id/imageButton10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/imageButton9"
android:src="@drawable/red" />
<ImageButton
android:id="@+id/imageButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/imageButton1"
android:src="@drawable/blue" />
<TextView
android:id="@+id/blueScore1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="@string/hint"
android:maxLength="2"
android:textIsSelectable="false"/>
<requestFocus />
</RelativeLayout>