android - 按下按钮时更改文本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>
解决方案
在您的 xmlLinearLayout
中,您有一个已将高度设置为:
android:layout_height="184dp"
里面有一个Button
高度:
android:layout_height="129dp"
因此,对于TextView
以下内容,存在最大值55dp
,但您可以设置:
android:layout_height="117dp"
这意味着至少有一半TextView
是不可见的。
在您的代码中,您TextView
在单击 时将随机文本设置为Button
。
你说什么都没发生。
是否存在正在设置文本但由于TextView
大部分隐藏而看不到它的情况?
改变视图的高度,你会发现。
推荐阅读
- ruby - Time.zone 适用于 API 但不适用于工作人员
- reactjs - React 全局状态更新本地状态
- c++ - 加载类 - 动态链接库返回未定义符号错误
- java - 为什么 volatile 在 java 中适用于像 ArrayList 这样的容器?
- database - 如何使用数据库在 Spring Mvc 中创建 REST Api
- c# - 服务器端处理动态列
- python - 这种语法有替代品吗?
- javascript - 为什么在 http 请求获取和响应中需要两个 `Promise.all` 实例?
- python-3.x - 创建递归函数以及封闭函数定义
- javascript - Laravel 服务器未收到附加到 FormData 的文件