0

我编写了这段代码来创建一个更改字体的按钮,但我不工作,如果条件从未变为真,我的问题是什么?

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);
            }
        }
    });
4

5 回答 5

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);
        }
    }
});
于 2013-08-27T05:34:50.167 回答
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);
        }
    }
});

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

于 2013-08-27T05:04:50.347 回答
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);
        }
    }
});
于 2013-08-27T05:08:17.227 回答
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);
            }
        }
    });
于 2013-08-27T05:13:06.437 回答
0

要更改字体,首先需要将字体放到 Asset 文件夹中。

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

主要的.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);
        }
    }

}
于 2013-08-27T05:29:26.910 回答