【问题标题】:How to set TextView to other random TextView in same class?如何将 TextView 设置为同一类中的其他随机 TextView?
【发布时间】:2013-04-26 01:05:50
【问题描述】:

我想将我的 Android 应用程序中的 TextView 随机设置为同一类中的另一个 TextView。我试过这个:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pop = (TextView) findViewById(R.id.popUSA);
poprank = (TextView)findViewById(R.id.poprankUSA);
randomUSA = (TextView) findViewById(R.id.randomUSA);
Random randomStat = new Random();
int random = (randomStat.nextInt(2));

if (random == 1){
randomUSA.setText(pop +"");
if (random == 2){
randomUSA.setText(poprank+"");          
}};

}

但它没有用。它会向我显示一些随机文本,并且在我重新打开应用程序时并没有改变。另外,有人可以告诉我如何制作一个按钮来刷新 TextView 并将其设置为不同的随机按钮。 谢谢你

【问题讨论】:

    标签: android text random textview


    【解决方案1】:

    首先,

    执行此操作时,您正在设置对象的引用 ID:

    randomUSA.setText(pop +"");
    

    这就是为什么你会收到奇怪的文字。

    改用这个,

    randomUSA.setText(pop.getText()) 
    

    第二,

    在我看来,Random.nextInt(2) 永远不会返回 2,它只会返回 0 或 1。 如果您正在寻找 1 或 2 作为结果,请改用 (randomStat.nextInt(2) + 1)。

    第三,

    使用 ID 引用您的按钮,并设置点击监听器,

    Button refresherButton = (Button) findViewById(R.id.refersherButton);
    refresherButton .setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            randomUSA.setText(pop.getText());
        }
    });
    

    更新您的代码:

    setContentView(R.layout.activity_main);
    pop = (TextView) findViewById(R.id.popUSA);
    poprank = (TextView) findViewById(R.id.poprankUSA);
    
    // I would suggest you to pre-set something into this two textview,
    // so that you can get it later.
    // But of course you should set it base on your application logic, not hardcode
    pop.setText("Population: 316,668,567");
    poprank.setText("Population Rank: 3");
    
    randomUSA = (TextView) findViewById(R.id.randomUSA);
    bRandomusa = (Button) findViewById(R.id.bRandom);
    Random randomStat = new Random();
    
    int firstRandom = randomStat.nextInt(2);
    if (firstRandom == 0) {
        randomUSA.setText(pop.getText());
    }else if (firstRandom == 1) {
        randomUSA.setText(poprank.getText());
    }
    
    //Use setTag here, for cleaner codes
    bRandomusa.setTag(randomStat);
    bRandomusa.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Random randomStat = (Random) v.getTag();
            int random = randomStat.nextInt(2);
            if (random == 0) {
                randomUSA.setText(pop.getText());
            }else if (random == 1) {
                randomUSA.setText(poprank.getText());
            }
        }
    });
    

    【讨论】:

    • 我尝试使用 .gettext 代替,但现在没有任何显示。另外,对于按钮,您说要使用 randomUSA.setText(pop +"");但在你说要使用 pop.gettext 之前。我应该使用哪一个。
    • 让我们从头开始,pop和poprank textview里面有什么吗?如果 .gettext 什么都没有显示,我相信两个 textview 里面什么都没有。如果没有你想在 randomUSA.setText(pop +"") 中参考你的代码来完成什么?
    • 另一个问题,当我将 if 语句放在按钮的 onclicklistener 中时,它给了我一个错误,说我不能将非最终变量放在以不同方法定义的内部类中。我是否将 if 语句放在了错误的位置?
    • 在这些行中,我正在检查随机值的值是 1 还是 2。如果是 1,它将显示 pop,如果是 2,它将显示 poprank。然后我将这些行放在按钮内,因为我认为它只会在按下按钮后检查数字。
    • 我相信错误发生在您的随机对象上?您可以通过将其声明为 final Random randomStat = new Random() 来轻松解决它,对于 1 或 2 种情况,我只是提醒您 randomStat.nextInt(2) 永远不会返回 2。
    猜你喜欢
    • 2019-06-09
    • 2012-11-13
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多