简体   繁体   English

Android:如何根据用户输入动态更改按钮文本?

[英]Android: How to dynamically change button text based on user input?

I want to create a button that changes its text based on user input. 我想创建一个按钮,根据用户输入更改其文本。 So it would say "NAME" and then the user would click it and be able to edit it as if it was a TextView. 所以它会说“NAME”,然后用户会点击它并能够编辑它,好像它是一个TextView。 I have no idea how to place a TextView on top of a button perfectly which is why I am in need of this solution. 我不知道如何将TextView完美地放在按钮上,这就是我需要这个解决方案的原因。 This is my button: 这是我的按钮:

    <Button
        android:id="@+id/name1"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="Name"/>

Thanks! 谢谢!

You can do it with this code: 您可以使用以下代码执行此操作:

Button btn = (Button) findViewById(R.id.name1);
// lets say you have an EditText for your user input
EditText et = (EditText) findViewById(R.id.input);

btn.setOnClickListener(new OnClickListener(){

btn.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        btn.setText(et.getText().toString());
    }
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM