简体   繁体   English

在android studio中按下按钮后,如何更改其文本?

[英]How do I change my button's Text after it is pressed in android studio?

I'm learning as I go and am trying to create a simple app. 我正在学习,正在尝试创建一个简单的应用程序。 I've run into this snag where I am setting up an event listener/callback method and want it to change the text of my button to something else. 我遇到了这个麻烦,在这里我设置了一个事件侦听器/回调方法,并希望它可以将按钮的文本更改为其他内容。

Here's a snippet of the code: 这是一段代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    startButton = (TextView) findViewById(R.id.startButton);
    mainMessage = (TextView) findViewById(R.id.mainMessage);
    InText = (TextView) findViewById(R.id.InText);

    final Button startButton = (Button)findViewById(R.id.startButton);


    this.gestureDetector = new GestureDetectorCompat(this, this);
    gestureDetector.setOnDoubleTapListener(this);

    startButton.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View v){
                    startButton = setText("change my text");

                }



            }
    );

On the startButton = setText("change my text"); startButton = setText("change my text"); line, I am getting the error: 行,我得到了错误:

Cannot resolve method 'setText(java.lang.String)' 无法解析方法“ setText(java.lang.String)”

Im still new to coding and understand most of the basics. 我对编码仍然很陌生,并且了解大多数基础知识。 In this same activity, I have gestures that can change the text of a textView to something else so I'm kinda stumped as to why this isn't working. 在同一活动中,我的手势可以将textView的文本更改为其他内容,因此我有点困惑为什么它不起作用。

Change 更改

 startButton = setText("change my text");

to

 startButton.setText("change my text");

暂无
暂无

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

相关问题 在android studio上按下按钮时如何进行简单的文本交换 - How to make simple text swap when button pressed on android studio 当文本部分为空白并且按下按钮时(在 android 工作室中),如何显示 toast 消息? - how can I show toast message when text section is blank and the button gets pressed (in android studio)? Android Studio - 按下按钮后如何让我的 RecyclerView 包含更多项目? - Android Studio - How do I make my RecyclerView contain more items after a button press? 如何更改按钮状态直到按下另一个按钮? - How do I change a button state until another button is pressed? 按下按钮后如何更改应用程序界面的文本? - How do I alter text of an app interface after a button has been pressed? 如何通过按钮更改Android studio中开关的state? - How do I change the state of a switch in Android studio via a button? 按下按钮后,如何使GUI驱动程序类中的变量转移到ActionListener类中? - How do I make the the variables in my GUI Driver class to be transferred to the ActionListener class after a button is pressed? 我无法向我的应用程序中的下一个按钮添加功能。 每次按下时,它应该更改文本。 - I cannot add functionality to the next button in my app. It should change the text every time it's pressed. 按下按钮直到用户点击EditText框,如何隐藏Android小键盘? - How do I hide Android keypad after a button is pressed until user taps an EditText box? 按下按钮后如何更改按钮的文本 - how to change the text of a button once that button is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM