以用户身份更新文本视图 单击它

发布于 2025-01-08 17:30:50 字数 307 浏览 1 评论 0原文

我的 Linearlayout 中有 8 个 TextView,我想要实现的是,一旦用户单击任何 TextView,它就应该更新为它下面的下一个 Textview ... 我正在尝试制作简单的游戏,其中图像将随机显示,用户必须从列表中找到显示为不同 8 TextView 的图像名称... 例如:如果有 8 个 TextView,其中包含 Dog、Cat、Bike、Horse、Cow 等。并且说 Cat 的图像已显示,那么我希望 cat 现在消失,每个 TextView 都应该更新上面一行,以便我可以添加更多视图最后......

我希望不会感到困惑,因为我在这里很困惑......

I have 8 TextView in my Linearlayout and What I am trying to achieve is as soon as user clicks on any TextView it should get updated with the next Textview below it ...
I am trying to make simple game where Image will be randomly displayed and the user has to find that Image name from the List displayed as different 8 TextView...
eg : If there is 8 TextView with List Dog, Cat , Bike , Horse, Cow etc.. and say Image of Cat has shown up then I want cat to disapper now every textview should get updated one row above so I can add more views at the end ...

I hope am not confusing because I am confused here ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淑女气质 2025-01-15 17:30:50

如果您将所有 TextView 放入数组或列表中,那么它就变成了简单的数组操作:

public void onClick(View pressed){ 
    boolean tvFound = false; //have we found the clicked tv yet?
    for(int i=0;i<mTextViews.length;i++){ //loop through all tvs
        if(pressed.getId() == mTextViews[i]){ //if this is the tv that was clicked on
            tvFound = true; 
        }
        if(tvFound){ // if we have found the tv that was clicked on
            if(i<mTextViews.length-1){ //if we arent on the last tv
                mTextViews[i].setText(mTextviews[i+1].getText()); //then replace txt with next tvs text
            }else{ //if we ARE on the last tv
                mTextViews[i].setText(""); //replace text with nothing
            }
        }
    }
}

我还想指出,如果您不想循环所有 TextView 来查找被单击的 TextView,那么您可以可以将每个 TextView 的 ResId 映射到其在数组中的索引,然后您可以只操作必要的 TextView。

If you place all of the TextViews in an Array or a List, then it becomes simple array manipulation:

public void onClick(View pressed){ 
    boolean tvFound = false; //have we found the clicked tv yet?
    for(int i=0;i<mTextViews.length;i++){ //loop through all tvs
        if(pressed.getId() == mTextViews[i]){ //if this is the tv that was clicked on
            tvFound = true; 
        }
        if(tvFound){ // if we have found the tv that was clicked on
            if(i<mTextViews.length-1){ //if we arent on the last tv
                mTextViews[i].setText(mTextviews[i+1].getText()); //then replace txt with next tvs text
            }else{ //if we ARE on the last tv
                mTextViews[i].setText(""); //replace text with nothing
            }
        }
    }
}

I also want to note that if you don't want to loop through all of the TextViews to find the one that was clicked on, you can map the ResId of each TextView to its index in the array, then you can manipulate just the necessary TextViews.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文