【原创】利用typeface实现不同字体的调用显示及String转换为Unicode

时间:2024-04-08 21:35:32

最近工作用到,就写个小demo

demo实现从assets中利用typeface调用不同字体,并在editText中显示出来

1.layout中创建activity_main.xml文件

布局代码如下:

 <LinearLayout 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"
android:background="#ffffff"
android:orientation="vertical"> <EditText
android:id="@+id/edit_test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#eeeeee"
android:focusable="true"
android:hint="字体1" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="39dp"
android:layout_marginTop="14dp"
android:text="字体2" /> <EditText
android:id="@+id/edit_test2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="#eeeeee"
android:focusable="true"
android:hint="字体1" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="39dp"
android:layout_marginTop="14dp"
android:text="字体2" /> </LinearLayout>

Java代码如下:

MainActivity.java

 package com.example.edittest;

 import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast; public class MainActivity extends Activity {
private EditText editText1;
private EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} public void initView() {
editText1 = (EditText) findViewById(R.id.edit_test1);
editText2 = (EditText) findViewById(R.id.edit_test2); //调用Typeface的createFromAsset方法 这里用两个ttf文件,ziti1.ttf、ziti2.ttf,这里用的是getAssets()这个方法
//字体1 路径为 assets目录下创建的fonts文件夹: /assets/fonts/ziti1.ttf
Typeface typeface1 = Typeface.createFromAsset(getAssets(),
"fonts/ziti1.ttf");
editText1.setTypeface(typeface1); // 监听回车键
editText1.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
Toast.makeText(MainActivity.this, String.valueOf(actionId),
Toast.LENGTH_SHORT).show();
return false;
}
});
//字体2 路径为 assets目录下创建的fonts文件夹: /assets/fonts/ziti2.ttf
Typeface typeface2 = Typeface.createFromAsset(getAssets(),
"fonts/ziti2.ttf");
editText2.setTypeface(typeface2); // 监听回车键
editText2.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
Toast.makeText(MainActivity.this, String.valueOf(actionId),
Toast.LENGTH_SHORT).show();
return false;
}
}); Button getValue1 = (Button) findViewById(R.id.button1);
Button getValue2 = (Button) findViewById(R.id.button2); getValue1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Toast显示字符转换的Unicode码
Toast.makeText(MainActivity.this,
string2Unicode(editText1.getText().toString()),
Toast.LENGTH_SHORT).show();
}
});
getValue2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Toast显示字符转换的Unicode码
Toast.makeText(MainActivity.this,
string2Unicode(editText2.getText().toString()),
Toast.LENGTH_SHORT).show();
}
});
} // 字符串转换成Unicode
public static String string2Unicode(String string) {
StringBuffer unicode = new StringBuffer();
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
unicode.append("\\u" + Integer.toHexString(c));
}
return unicode.toString();
}
}

最后在edittext中输入字符后,点击button可分别显示出对应的Unicode码。

图和demo我都不上了,很简单的一个例子,希望能帮到需要的同学。