【问题标题】:How to make a button change a label [duplicate]如何使按钮更改标签[重复]
【发布时间】:2016-06-30 16:38:09
【问题描述】:

我是 java 新手,我正在制作一个小游戏,当用户按下按钮时会说“是”或“否”。我不确定如何编写代码以根据用户按下按钮来更改标签。代码如下。您在代码中看到的任何其他问题,我也有兴趣发现这些问题。

public class FirstProject extends AppCompatActivity {

TextView answerTextView;
EditText name1Txt;
EditText name2Txt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        answerTextView = (TextView) findViewById(R.id.answerLbl);
        name1Txt = (EditText) findViewById(R.id.nameoneTxt);
        name2Txt = (EditText) findViewById(R.id.name2Txt);

        Button compBtn = (Button) findViewById(R.id.compBtn);
        compBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            //where I want to make the button react

            }
        });
    }

【问题讨论】:

  • 在发布问题之前阅读文档

标签: java android button label


【解决方案1】:

每个 EditText 都有一个你必须调用的方法

EditText#setText(...);

示例:

 name1Txt = (EditText) findViewById(R.id.nameoneTxt);
        name2Txt = (EditText) findViewById(R.id.name2Txt);

        Button compBtn = (Button) findViewById(R.id.compBtn);
        compBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            //where I want to make the button react
            name1Txt.setText("Hello..."); 
            name2Txt.setText("...world"); 
            }
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 2020-10-21
    • 1970-01-01
    相关资源
    最近更新 更多