-5

我想以EditText粗体显示onClick按钮的文本,但是当我在模拟器中运行应用程序时,下面的代码给了我一个错误The application chatApp has stopped unexpectedly,但是当我取出这段代码时它工作正常。

代码

 private void setBoldText() {
    final EditText sendMessage = (EditText) findViewById(R.id.myMessage);
    Button boldButton = (Button) findViewById(R.id.btnBold);
    boldButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            sendMessage.setTypeface(null, Typeface.BOLD);

        }
    });

}
4

3 回答 3

0

试试..这段代码...

public class MainActivity extends Activity {


EditText ed;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ed=(EditText)findViewById(R.id.editText1);
    btn=(Button)findViewById(R.id.button1);


    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

             Typeface tf = Typeface.createFromAsset(getAssets(),
                        "fonts/your.ttf");
               ed.getText().toString();
                ed.setTypeface(tf);

        }
    });
}

将您的自定义字体放在 assets-> fonts 文件夹中...希望这会帮助您...它正在工作...

于 2013-10-14T11:48:59.793 回答
0

下面的代码将起作用

edit.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD);
于 2013-10-14T11:51:30.110 回答
0
<style name="tp" >
   <item name="android:textColor">#ff0000</item>
   <item name="android:textStyle">bold</item>
   <item name="android:textSize">17sp</item>
</style>  



 Button boldButton = (Button) findViewById(R.id.btnBold);
boldButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        sendMessage.setTextAppearance(ClassName.this, R.style.tp);

    }
});
于 2013-10-14T11:52:34.083 回答