首页 > 解决方案 > 按下按钮时更改文本android studio

问题描述

我在 youtube 上尝试了一个教程,但它不起作用,当我单击按钮时没有任何反应。当我按下按钮时,我想更改布局活动并在新布局上设置一个随机文本,让我们说其他 10 个,在这个当前布局上,我想要一个按钮,它将第一个文本随机更改为另一个。这是我使用过但没有解决的代码。

@我无法添加代码,因为它不允许我将其发布在这里..它说类似无效的内容,但您可以看到图片中的所有内容,所以我做了所有代码,当我按下按钮时什么都不做,可能是什么问题?如果你有任何想法或者你知道任何教程来帮助我实现我所说的我想做的事情,我将非常感激

@你可以看到有一个字符串选项,当我点击我的按钮时,它应该给我所有的文本,但它没有

抱歉图片,但这些代码似乎不好

   <LinearLayout
    android:layout_width="332dp"
    android:layout_height="184dp"
    android:layout_marginStart="36dp"
    android:layout_marginLeft="36dp"
    android:layout_marginTop="48dp"
    android:orientation="vertical"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">


    <Button


        android:id="@+id/buttonsports"
        android:layout_width="match_parent"
        android:layout_height="129dp"
        android:background="@drawable/ft"
        tools:layout_editor_absoluteX="115dp"
        tools:layout_editor_absoluteY="291dp" />


    <TextView
        android:id="@+id/sports"
        android:layout_width="216dp"
        android:layout_height="117dp"
        android:text="TextView"
        tools:layout_editor_absoluteX="95dp"
        tools:layout_editor_absoluteY="246dp" />
</LinearLayout>

这是我的文本框有多大,“sports”是 TextView 的 id,即“abc”

标签: androidandroid-layoutandroid-activityonclickandroid-button

解决方案


在您的 xmlLinearLayout中,您有一个已将高度设置为:

android:layout_height="184dp"

里面有一个Button高度:

android:layout_height="129dp"

因此,对于TextView以下内容,存在最大值55dp,但您可以设置:

android:layout_height="117dp"

这意味着至少有一半TextView是不可见的。
在您的代码中,您TextView在单击 时将随机文本设置为Button
你说什么都没发生。
是否存在正在设置文本但由于TextView大部分隐藏而看不到它的情况?
改变视图的高度,你会发现。


推荐阅读