java– 单击按钮后如何显示文本

java– 单击按钮后如何显示文本,第1张

概述我正在尝试创建一个简单的应用程序,当您单击按钮文本时出现.但我有一个问题,当我点击按钮没有任何反应.publicclassMainActivityextendsAppCompatActivity{privateTextViewtextView;privateButtonbutton;@OverrideprotectedvoidonCreate(Bundle

我正在尝试创建一个简单的应用程序,当您单击按钮文本时出现.但我有一个问题,当我点击按钮没有任何反应.

public class MainActivity extends AppCompatActivity {    private TextVIEw textVIEw;    private button button;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        textVIEw = (TextVIEw) findVIEwByID(R.ID.textVIEw);        textVIEw.setMovementMethod(new ScrollingMovementMethod());        button = (button) findVIEwByID(R.ID.button);        VIEw.OnClickListener ourOnClickListener = new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                textVIEw.setText("It real sent your at. Amounted all shy set why followed declared. Repeated of endeavor mr position kindness offering ignorant so up. Simplicity are melancholy preference consIDered saw companions. disposal on outweigh do speedily in on. Him ham although thoughts entirely drawings. Acceptance unreserved.");            }        };    }}

XML

  <button        androID:ID="@+ID/button"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_marginBottom="84dp"        androID:layout_marginEnd="8dp"        androID:layout_marginStart="8dp"        androID:text="button"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent" />    <TextVIEw        androID:ID="@+ID/textVIEw"        androID:layout_wIDth="374dp"        androID:layout_height="331dp"        androID:layout_marginEnd="8dp"        androID:layout_marginStart="8dp"        androID:layout_margintop="16dp"        androID:textSize="8sp"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_totopOf="parent" />

解决方法:

您正在启动onClickListener,但您没有将它附加到任何对象.尝试将onClickListener附加到您的按钮,如下所示:

button.setonClickListener(ourOnClickListener);
总结

以上是内存溢出为你收集整理的java – 单击按钮后如何显示文本全部内容,希望文章能够帮你解决java – 单击按钮后如何显示文本所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1116247.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存