简体   繁体   English

如何在FrameLayout中设置TextView的文本?

[英]How do i set the text of a TextView in a FrameLayout?

I have a game that adds a point every time a player passes a ball through a goal. 我有一个游戏,每当一个球员通过一个球进球时,他就会增加一个点数。 I added a TextView that I want updated every time a goal and initialized it with the text "1" and gave it the id "scoreTextView". 我添加了一个TextView,我想在每次目标时更新它并用文本“1”初始化它并给它id“scoreTextView”。 I tried to declare a TextView variable in my main activity and using addView but my game crashes everytime.I have also tried passing in Layout params along with the TextView to be displayed after reading another StackOverflow question but that also caused my game to crash. 我试图在我的主要活动中声明一个TextView变量并使用addView,但我的游戏每次都崩溃。我也尝试在阅读另一个StackOverflow问题之后传递Layout params以及TextView,但这也导致我的游戏崩溃。 When I dont declare a TextView varible and dont add the scoreTextView to the mainView (that is the name of my FrameLayout varible) in my main activity the game actually runs and displays the 1 but if I can't access the scoreTextView from my activity I have no way to change it. 当我没有声明TextView变量并且不在我的主要活动中将scoreTextView添加到mainView(这是我的FrameLayout变量的名称)时,游戏实际运行并显示1但是如果我无法从我的活动中访问scoreTextView我无法改变它。 So my question is how do I create a TextView in a FrameLayout that I actually can use and change dynamically as needed in my main activity? 所以我的问题是如何在FrameLayout中创建一个我实际可以使用的TextView并在我的主要活动中根据需要动态更改?

I have already looked at this and tried it but they didn't work: Add a Textview to a FrameLayout in a determined position 我已经看过这个并尝试了但是它们不起作用: 在确定的位置向FrameLayout添加Textview

<FrameLayout
    android:id="@+id/yourFrameLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/lin2"
    android:layout_below="@+id/lin1"
    android:orientation="vertical" >        

    <TextView
        android:id="@+id/messageTextView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="20sp"
        android:text="Put your message over here" />
</FrameLayout>

and then you can visible or invisible this messageTextView according to your need and this is quite easy way to access . 然后你可以根据你的需要显示或隐藏这个messageTextView,这是一种非常简单的访问方式。 hope it ll help you 希望它会帮助你

Maybe your problem is that you are trying to update TextView from non UI thread? 也许你的问题是你试图从非UI线程更新TextView The Log message would really help... 日志消息真的有帮助......

If this is the case, then try adding this: 如果是这种情况,请尝试添加以下内容:

runOnUiThread(new Runnable() {
    public void run() {
        // Update TextView here
TextView scoreTextView = (TextView)findViewById(R.id.scoreTextView);
scoreTextView.setText("Goals scored: " + goals);
    }
});
TextView textView = (TextView) findViewById(R.id.messageTextView);
textView.setText("this is where you add what you want the text to say");

This should be done in the activity class attached to the layout. 这应该在附加到布局的活动类中完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM