2010-10-03 81 views
28

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

+0

'((android.widget.Button)findViewById(R.id.epic_button))的setText( “mytitle”);' – 2013-09-15 18:53:41

+0

findViewById不适合工作小工具!这个方法在小部件中不受支持。 – coolcool1994 2014-08-26 07:22:33

+1

它的工作原理。只要确保在你的java文件的顶部添加'import android.widget.View;'。如果你还添加了'import android.widget.Button;',它可以缩写为:'((Button)findViewById(R.id.yourButtonName))。setText(“New Text”);' – 2016-01-27 06:25:25

回答

16

我能够改变按钮的文字是这样的:

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"); 
+2

我被困在同一个地方..你能粘贴代码吗? – Harshad 2011-03-25 06:21:54

+0

我如何获得按钮的文字。 – 2013-05-03 03:29:49

+0

谢谢,这节省了我一些时间!您还需要添加\t ns.notify(notificationID,notification); – PSIXO 2013-07-29 14:49:10

38

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

import android.widget.Button; 

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

而且,只是作为一个参考点,Button延伸TextView,因此为什么你可以使用setText()就像一个普通的TextView。

+7

但是在一个小部件findViewById不存在 – Skatephone 2010-10-03 22:06:59

+1

@Skatephone:然后,如果您发布了一些代码,它会更容易帮助您。这就是说,RemoteViews类有一个'setTextViewText()'方法。我建议看看这个:http://developer.android.com/reference/android/widget/RemoteViews.html#setTextViewText%28int,%20java.lang.CharSequence%29 – eldarerathis 2010-10-03 23:40:43

+0

@Skatephone:你不应该在Widget上调用findViewById,但在Activity或View上调用。 – benvd 2010-10-04 07:27:12

0

我在我的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.

工作很好!

0

这可能是脱离主题,但对于那些正在努力改变按钮文本的字体也是如此(这是我的情况和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的方法。下面的代码粘贴正是在上述

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你把你所需的字体

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

0

使用交换使用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); 
0

这是很容易

Button btn = (Button) findViewById(R.id.btn); 
btn.setText("MyText"); 
相关问题