【问题标题】:How to change the text of a Button widget in Android?Android小部件:如何更改按钮的文本
【发布时间】:2011-04-20 13:44:54
【问题描述】:

如何在代码而不是 XML 文件中更改 Android 按钮小部件的文本?

【问题讨论】:

  • ((android.widget.Button)findViewById(R.id.epic_button)).setText("mytitle");
  • findViewById 不适用于小部件!小部件不支持此方法。
  • 它有效。请务必在您的 java 文件顶部添加import android.widget.View;。如果还加上import android.widget.Button;,可以简写为:((Button)findViewById(R.id.yourButtonName)).setText("New Text");

标签: android button widget


【解决方案1】:

//文字按钮:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" text button" />

//彩色文本按钮:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text button" 
        android:textColor="@android:color/color text"/>

//背景按钮

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text button" 
        android:textColor="@android:color/white"
        android:background="@android:color/ background button"/>

// 文字大小按钮

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text button" 
        android:textColor="@android:color/white"
        android:background="@android:color/black"
        android:textSize="text size"/>

【讨论】:

  • 你确定吗?您的答案不包含任何代码?
【解决方案2】:

这很简单

Button btn = (Button) findViewById(R.id.btn);
btn.setText("MyText");

【讨论】:

    【解决方案3】:

    使用java进行交换。 setText = "...",对于java类还有更多的实现方法。

        //button fechar
        btnclose.setEnabled(false);
        btnclose.setText("FECHADO");
        View.OnClickListener close = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (btnclose.isClickable()) {
                    btnOpen.setEnabled(true);
                    btnOpen.setText("ABRIR");
                    btnclose.setEnabled(false);
                    btnclose.setText("FECHADO");
                } else {
                    btnOpen.setEnabled(false);
                    btnOpen.setText("ABERTO");
                    btnclose.setEnabled(true);
                    btnclose.setText("FECHAR");
                }
    
                Toast.makeText(getActivity(), "FECHADO", Toast.LENGTH_SHORT).show();
            }
        };
    
        btnclose.setOnClickListener(close); 
    

    【讨论】:

      【解决方案4】:

      我的 layout.xml 中有一个按钮,它被定义为视图,如下所示:

      final View myButton = findViewById(R.id.button1);
      

      在将其定义为按钮之前,我无法更改其上的文本:

      final View vButton = findViewById(R.id.button1);
      final Button bButton = (Button) findViewById(R.id.button1);
      

      当我需要更改文本时,我使用了bButton.setText("Some Text");,当我想要更改视图时,我使用了vButton.

      效果很好!

      【讨论】:

        【解决方案5】:

        这可能是题外话,但对于那些正在努力如何准确更改按钮文本字体的人(这是我的情况,Skatephone 的回答帮助了我)我是这样做的(如果你做了按钮 ind 设计模式):

        首先,我们需要将按钮的字符串名称从 xml 中“转换”(这是一种错误的解释方式,但直截了当)到 java,因此我们将上述代码粘贴到 MainActivity.java 中

        重要提示!将代码放在 OnCreate 方法下!

        import android.widget.RemoteViews;
        
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout);
        remoteViews.setTextViewText(R.id.Counter, "Set button text here");
        

        记住:

        my_layout 必须替换为按钮所在的 xml 文件

        Counter 必须替换为按钮的 id 名称 ("@+id/ButtonName")

        如果您想更改按钮文本,只需插入文本代替 "Set button text here"

        这里是你改变字体的部分:

        现在您已从 xml“转换”为 java,您可以为 TextView 设置一个 Typeface 方法。将以下代码完全粘贴在上面刚刚描述的上一个代码下

        TextView txt = (TextView) findViewById(R.id.text_your_text_view_id);
                Typeface font = Typeface.createFromAsset(getAssets(), "fonts/MyFontName.ttf");
                txt.setTypeface(font);
        

        text_your_text_view_id 的位置放置按钮的 ID 名称(如之前的代码),在 MyFontName.ttf 的位置放置所需的字体

        警告!这假设您已经将所需的字体放入 资产/字体文件夹。例如资产/字体/MyFontName.ttf

        【讨论】:

          【解决方案6】:

          您可以使用setText() 方法。示例:

          import android.widget.Button;
          
          Button p1_button = (Button)findViewById(R.id.Player1);
          p1_button.setText("Some text");
          

          另外,作为参考,Button 扩展了 TextView,因此您可以像使用普通 TextView 一样使用 setText()

          【讨论】:

          • 但是在一个小部件上 findViewById 不存在
          • @Skatephone:那么,如果您发布一点代码会更容易帮助您。话虽如此,RemoteViews 类有一个setTextViewText() 方法。我建议看一下:developer.android.com/reference/android/widget/…
          • @Skatephone:您不应该在 Widget 上调用 findViewById,而应在 Activity 或 View 上调用。
          • 当我收到在 onReceive 方法中按下的某个按钮时,我试试这个: RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main); remoteViews.setTextViewText(R.id.ButtonMeno, "-");
          • 这个解决方案对我有用,可以从 onClick 按钮调用的方法中找到其他“小部件”(视图)。谢谢。
          【解决方案7】:

          我能够像这样更改按钮的文本:

          import android.widget.RemoteViews;
          
          //grab the layout, then set the text of the Button called R.id.Counter:
          RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.my_layout);
          remoteViews.setTextViewText(R.id.Counter, "Set button text here");
          

          【讨论】:

          • 如何获取按钮的文本。
          • 谢谢,这节省了我一些时间!您还需要添加 ns.notify(notificationID, notification);
          • 我也想知道,如何在小部件内设置按钮的文本。使用 remoteViews.setTextViewText(R.id.MyButton, "sometext") 对我不起作用
          • 问题是,当我使用 onReceive() 方法(它用于小部件中的按钮事件侦听器)时,RemoteView 为空,或者即使我在通过 onUpdate( )。
          • 成功了!非常感谢!
          猜你喜欢
          • 1970-01-01
          • 2021-07-07
          • 2013-08-28
          • 2019-03-07
          • 1970-01-01
          • 2020-05-08
          • 1970-01-01
          • 2020-05-13
          • 1970-01-01
          相关资源
          最近更新 更多