Java 我可以在Android视图中使用类方法吗?

Java 我可以在Android视图中使用类方法吗?,java,android,class,view,Java,Android,Class,View,我是Android(Java)新手,我的课程之一就是创建这个场景。 他们教我对每个按钮使用不同的方法,但这增加了两种几乎相等的方法,我的头脑干燥,说这不是最好的解决办法 我写了一个篮球队课程: package com.example.android.courtcounter; import android.view.View; import android.widget.TextView; public class BasketTeam { private TextView te

我是Android(Java)新手,我的课程之一就是创建这个场景。

他们教我对每个按钮使用不同的方法,但这增加了两种几乎相等的方法,我的头脑干燥,说这不是最好的解决办法

我写了一个
篮球队
课程:

package com.example.android.courtcounter;

import android.view.View;
import android.widget.TextView;

public class BasketTeam {

    private TextView team;

    public BasketTeam(View v) {
        team = (TextView) v;
    }

    public void threePointsThrow(View v) {
        this.addPoints(3);
    }

    public void twoPointsThrow(View v) {
        addPoints(2);
    }

    public void freeThrow(View v) {
        addPoints(1);
    }

    private void addPoints(Integer addedScore) {
        Integer teamScore = Integer.parseInt((String) team.getText());
        team.setText("" + (teamScore + addedScore));
    }

    public void resetScore() {
        team.setText("" + 0);
    }
}
在我的主要活动中,我创建了两个实例作为public
BasketTeam1
BasketTeam2
。 它们都是很好的实例,因为在我的主要活动中,我有一个名为
resetScore
的方法使用它们,它可以工作:

public void resetScore(View v) {
    BasketTeam1.resetScore();
    BasketTeam2.resetScore();
}
但在我看来,当我尝试使用其中一种类方法时,它找不到。 为什么?

下面是我尝试的一个例子:

<Button
    android:layout_height="wrap_content"
    android:layout_width="120dp"
    android:text="+3 points"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="16dp"
    android:id="@+id/team_1_3_points"
    android:background="@color/colorAccent"
    android:textColor="@android:color/white"
    android:onClick="BasketTeam1.threePointsThrow"/>

更新

这是错误消息:

05-25 13:30:19.880 2345-2345/com.example.android.courtcounter E/AndroidRuntime:FATAL EXCEPTION:main 进程:com.example.android.courtcounter,PID:2345 java.lang.IllegalStateException:在android的父或祖先上下文中找不到方法BasketTeam1.threePointsThrow(视图):在视图类android.support.v7.widget.AppCompatButton上定义的onClick属性,id为“team_1_3_points”


按钮的
onClick
属性必须对应于按钮上下文中的方法,通常是您的
MainActivity
。看

解决方案可以是:

public class MainActivity extends Activity {
    ...
    public void threePointsThrow(View v) {
        switch (v.getId()) {
            case R.id.team_1_3_points:
                basketTeam1.threePointsThrow();
                break;
            case R.id.team_2_3_points:
                basketTeam2.threePointsThrow();
                break;
            default:
                break;
        }
    }
}
在布局中:

<Button
android:layout_height="wrap_content"
android:layout_width="120dp"
android:text="+3 points"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:id="@+id/team_1_3_points"
android:background="@color/colorAccent"
android:textColor="@android:color/white"
android:onClick="threePointsThrow"/>

按钮的
onClick
属性必须对应于按钮上下文中的方法,通常是您的
MainActivity
。看

解决方案可以是:

public class MainActivity extends Activity {
    ...
    public void threePointsThrow(View v) {
        switch (v.getId()) {
            case R.id.team_1_3_points:
                basketTeam1.threePointsThrow();
                break;
            case R.id.team_2_3_points:
                basketTeam2.threePointsThrow();
                break;
            default:
                break;
        }
    }
}
在布局中:

<Button
android:layout_height="wrap_content"
android:layout_width="120dp"
android:text="+3 points"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:id="@+id/team_1_3_points"
android:background="@color/colorAccent"
android:textColor="@android:color/white"
android:onClick="threePointsThrow"/>


我认为你需要发布你的查看代码。有点奇怪,你居然把分数保存在textview中。我的MVC意识告诉我,最好将数据保存在模型中,并在更改时更新视图。。。另一点:为什么要将视图传递给
xxxPointsThrow
方法?你似乎没有使用它…别介意我上面评论的最后一部分。它们是onClickHandler…查看现在显示的代码。没有放足够的空间@Fildor我是Java和Android新手,我刚刚修改了教程中的代码,以尝试更好的方法。你能添加你收到的确切错误消息吗?我想你需要发布你的查看代码。有点奇怪,你实际上将分数保存在textview中。我的MVC意识告诉我,最好将数据保存在模型中,并在更改时更新视图。。。另一点:为什么要将视图传递给
xxxPointsThrow
方法?你似乎没有使用它…别介意我上面评论的最后一部分。它们是onClickHandler…查看现在显示的代码。没有放足够的空间@Fildor我是Java和Android新手,我刚刚修改了教程中的代码,以尝试更好的方法。你能添加你得到的确切错误消息吗?