在Android Studio中更新textView

战神

我在主要活动上设置了一个textView,还有一个按钮。当我单击按钮时,我希望textView根据以下代码开始更新其值。但是,这不起作用,问题是循环。有人可以解释为什么吗?我是Java和Android开发的新手

button2 = (Button) findViewById(R.id.button);
    button2.setOnClickListener(new OnClickListener() {
        TextView textView = (TextView)findViewById(R.id.refView);
        public void onClick(View arg0) {
            for(i=1;i<1;i++){
              i = i + 1;
            textView.setText(String.valueOf(i)+"hello");
            }
        }
    });

谢谢你

codeMagic

不知道到底要发生什么但是,您可以摆脱这一行

i = i + 1;

因为随着的每次迭代,i++已经增加i1 for loop

其次,由于i从1开始,并且您希望loop运行时i<1,它永远不会进入loop它永远不会小于1。

第三,如果条​​件不同,说

for (int i=0; i<10; i++)

它会运行得loop如此之快,以至于您甚至都不会意识到变化。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android Studio:第二活动中的TextView无法更新

Android BroadcastReceiver onReceive在MainActivity中更新TextView

Asynctask 不更新 Fragment 中的 TextView(Android)

如何从Android中的countdowntimer更新textview?

我在Android中的TextView没有更新

如何使TextView充当Android Studio中的按钮?

Textview属性未在Android Studio中显示

Android Studio 中的 TextView 未显示

在 Android Studio 中膨胀 TextView 时出错

在android studio中更新recyclerview

更新Android TextView

Android TextView无法更新

在Android中每秒更新一次TextView

如何在Android中输入edittext时更新textview

Android:如何在滑动视图同级片段中更新TextView?

如何使用Databinding android在editext中更新textview,ontextchanged

如何从要在Android中更新的Textview替换颜色?

Android - 无法在 View Pager Adapter 中更新 textview

用白色图像隐藏Android Studio中的所有textView

在TextView的内容没有显示(Android Studio中)

在Android Studio(Kotlin)中自定义TextView(文本外观)

如何在Android Studio中避免TextView上方的边距?

我的textview在android studio中不起作用

Android Studio 错误文本在 RecyclerView 中设置为 TextView

在android studio上的textview中读取html对象

如何让 Android Studio 在 textView 中显示计算值

Android Studio-更改textView中的字符串

在 Android Studio 中为 TextView 添加背景颜色

Android Studio-TextView未在设计视图布局中显示