2013-06-20 37 views
0

有没有什么办法可以将第一个字符的按钮文本设置为大写字母,并且所有文本都是上限?如何将按钮文本设置为大写字母

布局:

<Button 
    android:id="@+id/q1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="13sp" 
    android:textColor="#ffffff" 
    android:background="@drawable/ic_btn_q_normal" /> 

活动:

final Button q1 = (Button) findViewById(R.id.q1); 
q1.setText(answers[numbers.get(0)]); 
    q1.setOnClickListener(new OnClickListener() { 
     public void onClick(View v){ 

     q1.setBackgroundResource(R.drawable.ic_btn_q_right); 

    } 
}); 

答案[numbers.get(0)]是,我从数组列表获取文本。

我用q1.setAllCaps(true)尝试过;但它对我不起作用。

谢谢。

+0

以编程方式或与布局? – Blackbelt

+0

亲爱的黑带,以编程方式。谢谢。 – user1731690

+0

String.toUpperCase()呢? – Rob013

回答

1
final Button q1 = (Button) findViewById(R.id.q1); 
String label = answers[numbers.get(0)]; 
StringBuilder sb = new StringBuilder(); 
sb.append(label .substring(0,1)); 
sb.append(label .substring(1).toLowerCase()); 
label = sb.toString(); 

q1.setText(label); 
    q1.setOnClickListener(new OnClickListener() { 
     public void onClick(View v){ 

     q1.setBackgroundResource(R.drawable.ic_btn_q_right); 

    } 
}); 

代码字符串转换摘自:What is the simplest way to convert a Java string from all caps (words separated by underscores) to CamelCase (no word separators)?

2

您可以使用:WordUtils

方法:

利用(字符串str)

大写所有空格分隔w字符串中的ords。

或:capitalizeFully(字符串str)

转换所有的空格分隔单词串转化成大写话说,就是每个字是由一个标题字符,然后一系列的小写字符。

相关问题