0

我目前是一名高中生,为了好玩而决定尝试使用 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>    
4

1 回答 1

0

我想我可能理解你。如果我这样做,您可以使用一个标志来查看哪个Button被按下。所以流程就像

按下红色按钮 -> 标志 = 红色 -> 按下得分按钮 -> 红色得分 += 得分

在代码中就像

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;
         }
}

这显然完成得非常快,您必须根据需要调整变量。但是,如果我了解您想要什么,那么这将为您提供基本逻辑

于 2013-05-29T23:42:45.640 回答