创建一个用于更改字体的按钮

时间:2013-08-27 04:59:13

标签: android button

我写了这段代码来创建一个更改字体的按钮,但我不工作,如果条件永远不会成为现实,我的问题是什么?

font.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Typeface mitra = Typeface.createFromAsset(getAssets(), "font/MitraB.ttf");

            if (WBC.getTypeface() == mitra) {

                Typeface tabssom = Typeface.createFromAsset(getAssets(), "font/tabassom.ttf");
                WBC.setTypeface(tabssom);
            } else {
                WBC.setTypeface(mitra);
            }
        }
    });

5 个答案:

答案 0 :(得分:2)

试试这个:

 font.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        Typeface mitra = Typeface.createFromAsset(this.getAssets(),
            "font/MitraB.ttf");
        WBC.setTypeface(mitra); //edit
        if (WBC.getTypeface().equals(mitra)) {
             Typeface tabssom = Typeface.createFromAsset(this.getAssets(),
            "font/tabassom.ttf");
            WBC.setTypeface(tabssom);
        } else {
            WBC.setTypeface(mitra);
        }
    }
});

确保在项目文件夹中有一个资产文件夹,其中有一个字体文件夹。

答案 1 :(得分:2)

您可以直接将MitraB.ttf和tabassom.ttf放入资产文件夹本身并像这样调用它:

font.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View arg0)
    {
        Typeface t1 = Typeface.createFromAsset(getAssets(),"MitraB.ttf");

        if (WBC.getTypeface().equals(t1))
        {
            Typeface t2 = Typeface.createFromAsset(getAssets(),"tabassom.ttf");
            WBC.setTypeface(t2);
        }
        else
        {
            WBC.setTypeface(t1);
        }
    }
});

答案 2 :(得分:0)

试试这个......

 font.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        Typeface mitra = Typeface.createFromAsset(this.getAssets(),"font/MitraB.ttf");
        if (WBC.getTypeface().equals(mitra)) {
           Typeface tabssom = Typeface.createFromAsset(this.getAssets(),"font/tabassom.ttf");
            WBC.setTypeface(tabssom);
        } else {
            WBC.setTypeface(mitra);
        }
    }
});

答案 3 :(得分:0)

IF' mitra '是String然后与equals方法比较==运算符

 font.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Typeface mitra = Typeface.createFromAsset(this.getAssets(),"font/MitraB.ttf");
            if (WBC.getTypeface().equals(mitra)) {
               Typeface tabssom = Typeface.createFromAsset(this.getAssets(),"font/tabassom.ttf");
                WBC.setTypeface(tabssom);
            } else {
                WBC.setTypeface(mitra);
            }
        }
    });

答案 4 :(得分:0)

要更改字体,首先需要将字体放入资源文件夹。

下面的示例代码将显示,当单击“更改字体”按钮时,它会将TextView的字体更改为“KRISTENC.TTF”。请注意,字体名称与资产文件夹中的字体名称完全匹配。

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/txtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/btnFont"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/txtView"
        android:layout_marginRight="42dp"
        android:layout_marginTop="57dp"
        android:text="Change Font" />

</RelativeLayout>

MainActivity.java类

package com.font.fonttest;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{

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

        Button btnFont = (Button) findViewById(R.id.btnFont);  
        btnFont.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.btnFont:

            // Please put the Font KRISTENC.TTF to Assets folder 
            Typeface font = Typeface.createFromAsset(getAssets(), "KRISTENC.TTF");  

            // TextView to show the result
            TextView textView1 = (TextView)findViewById(R.id.txtView);
            // When click the button change the font type to KRISTENC.TTF
            textView1.setTypeface(font);
        }
    }

}