【问题标题】:Generate a random number and results appear in TextView生成随机数,结果出现在TextView中
【发布时间】:2017-08-30 03:56:00
【问题描述】:

我会尽量做到具体。这是我的第一篇文章/问题。

  1. 不,这不适用于与学校或工作相关的项目。
  2. 我研究并尝试了许多我看到的东西,但收效甚微。

我正在尝试在 Main 活动上创建一个按钮,该按钮生成一个随机数并将结果发送到与同一活动上的按钮相邻的 Textview。我唯一要更改的是 TextView 的内容。

这是我的 Java/Main Activity 代码:

package com.example.dannykennedyjr.diceroller;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void d4Results(View view) {
    TextView tv1 = (TextView) findViewById(R.id.d4TextView);
    final Random random = new Random();
    int d4Roll = random.nextInt(4)+1;
    tv1.setText(d4Roll);
}

}

这当然行不通,我开始工作的任何事情都会打开一个新的 TextView 并清除当前布局。

还有我的 XML:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:background="@android:color/black"
    android:onClick="d4Results"
    android:text="@string/roll_d4"
    android:textAlignment="center"
    android:textColor="@android:color/holo_red_dark"
    android:textSize="24sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:ems="10"
    android:hint="@string/roll_result"
    android:inputType="textPersonName"
    android:textAlignment="center"
    android:textSize="24sp"
    app:layout_constraintBaseline_toBaselineOf="@+id/button"
    app:layout_constraintHorizontal_bias="0.508"
    app:layout_constraintLeft_toRightOf="@+id/button"
    app:layout_constraintRight_toRightOf="parent" />

任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 试试tv1.setText(d4Roll + "") setText 需要字符串,不能取整数。我的建议是将d4Roll 与空字符串连接起来,使其自身成为一个字符串。
  • @ImmersionULTD "needs a string" 有点误导。 See my answer here 但是,是的,为此需要一个字符串
  • 我刚刚注意到我的 R.id.d4TextView 不匹配 android:id="@+id/textview"我更新了它,它在模拟器中运行,然后当我点击时崩溃D4 按钮。
  • 感谢 cmets 我会尝试一些 toString 调整
  • 还有一点解释,为什么在这里需要一个字符串:因为同样存在的 TextView.setText(int) 方法用于将字符串 ID 传递给 TextView(例如 R.string.应用程序名称)。所以如果你给它传递一个int,那不是字符串资源的id,它会混淆——甚至可能崩溃。

标签: java android random textview mobile-application


【解决方案1】:

我最近构建了一个 Android 猜谜游戏.... 这里是我如何在 TextView 中放置随机数。

int random1 = rand.nextInt(100) + 1; //generate random numbers from 1-100

n1.setText(Integer.toString(random1)); //n1 is a TextView

希望对你有所帮助。

【讨论】:

  • @JamesKennedyJr 如果这是正确答案,请标记此权利。谢谢
【解决方案2】:

您正在尝试将整数设置为字符串。你需要先转换它。 根据您的示例:

public void d4Results(View view) {
   TextView tv1 = (TextView) findViewById(R.id.d4TextView);
   final Random random = new Random();
   int d4Roll = random.nextInt(4)+1;
   tv1.setText(Integer.ToString(d4Roll));
}

【讨论】:

    【解决方案3】:

    此链接将为您提供所需的帮助:

    Generate Random Number

    像这样在TextView 中设置 int :

    tv1.setText(String.valueOf(d4Roll));
    

    【讨论】:

      猜你喜欢
      • 2017-04-12
      • 2015-02-16
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多