我目前是一名高中生,他决定尝试将Android开发用于娱乐,但我很难过。我有蓝队和红队的图像按钮。蓝队自动得分上升。我不知道怎么做的是当你点击红色按钮时,图像按钮会使红队得分上升,反之亦然。
这是我的java代码 package 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>
答案 0 :(得分:0)
我想我可能理解你。如果我这样做,您可以使用标记来查看按下了Button
。所以流程就像
按下红色按钮 - &gt; flag = red - &gt;按下分数按钮 - &gt;红色得分+ =得分
在代码中它就像
String flag = "";
public void onRedBtnClick(View v)
{
flag = "red";
}
public void scoreBtnClick(View v)
{
if (!flag.equals(""));
{
if ("red".equals(flag))
{
redScore += score;
}
if ("blue".equals(flag))
{
blueScore += score;
}
}
这显然是非常快速的,您必须根据需要调整变量。但如果我明白你想要什么,那么这将给你基本的逻辑