Android 从textView获取int

Android 从textView获取int,android,Android,我试图从文本视图中获取int值,其中的值总是一个字符串中的数字,我这样设置它 int randomNumber = rng.nextInt(6) + 1; switch (randomNumber) { case 1: imageViewDice.setImageResource(R.drawable.dice1); textView.setText("Tiraste un 1!"); if (pl

我试图从文本视图中获取int值,其中的值总是一个字符串中的数字,我这样设置它

int randomNumber = rng.nextInt(6) + 1;
    switch (randomNumber) {
        case 1:
            imageViewDice.setImageResource(R.drawable.dice1);
            textView.setText("Tiraste un 1!");

            if (player1.getText().equals("")) {
                player1.setText("1");
            } else {
                player2.setText("1");
            }
            break;
//rest of cases 2, 3, 4, 5, & 6
//this doesnt work 
int value1 = Integer.parseInt(player1.getText().toString());
int value2 = Integer.parseInt(player2.getText().toString());

if (value1 > value2) {
    result.setText("Jugador 1");
} else if (value1 < value2) {
    result.setText("Jugador 2");
} else {
    result.setText("Empate!");
}
我需要这个的int值来比较,然后看看哪个玩家(1或2)拥有最高的数字,类似这样的东西

int randomNumber = rng.nextInt(6) + 1;
    switch (randomNumber) {
        case 1:
            imageViewDice.setImageResource(R.drawable.dice1);
            textView.setText("Tiraste un 1!");

            if (player1.getText().equals("")) {
                player1.setText("1");
            } else {
                player2.setText("1");
            }
            break;
//rest of cases 2, 3, 4, 5, & 6
//this doesnt work 
int value1 = Integer.parseInt(player1.getText().toString());
int value2 = Integer.parseInt(player2.getText().toString());

if (value1 > value2) {
    result.setText("Jugador 1");
} else if (value1 < value2) {
    result.setText("Jugador 2");
} else {
    result.setText("Empate!");
}
//这不起作用
int value1=Integer.parseInt(player1.getText().toString());
int value2=Integer.parseInt(player2.getText().toString());
如果(值1>值2){
结果:setText(“Jugador 1”);
}否则如果(值1<值2){
结果:setText(“Jugador 2”);
}否则{
result.setText(“Empate!”);
}

获取值以便进行比较的正确方法是什么?

如果设置static,则可以很容易地在不同的类中读取:

player1.setText(Fraction.getP1()+"");
player2.setText(Fraction.getP2()+"");

if(Fraction.getP1()>Fraction.getP2())
   result.setText("Jugador 1");
新类

public class Fraction {

    private static int p1=0;
    private static int p2=0;

    public static int getP1(){
        return p1;
    }
    public static int getP2(){
        return p2;
    }
    public static void setP1(int p1fraction){
        Fraction.p1 = p1fraction; //Fraction is your class name
    }
    public static void setP2(int p2fraction){
        p2 = p2fraction;
    }

}

我认为您的代码在大多数情况下是两个播放器的文本视图具有相同的值,因为您在switch语句中将playerOne的初始值设置为player2,并且在代码中,值之间的比较将打印为空
if(player1.getText()==“”)
->@MahmoudAbuElheja它在做什么检查玩家一是否已经被填充如果没有,那么它填充玩家一,然后填充玩家二,我试图做的是取
“1”
,并将其转换为整数
1
,因此,这些值之间的比较将起作用,因为它们需要是int来检查一个值是否大于另一个值one@a_local_nobody是的,我刚才在我的代码中更改了,我将在question@Nancy可以添加完整的代码来显示textView之间的逻辑