标签:
最近工作用到,就写个小demo
demo实现从assets中利用typeface调用不同字体,并在editText中显示出来
1.layout中创建activity_main.xml文件
布局代码如下:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#ffffff" 6 android:orientation="vertical"> 7 8 <EditText 9 android:id="@+id/edit_test1" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:background="#eeeeee" 13 android:focusable="true" 14 android:hint="字体1" /> 15 16 <Button 17 android:id="@+id/button1" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:layout_marginRight="39dp" 21 android:layout_marginTop="14dp" 22 android:text="字体2" /> 23 24 <EditText 25 android:id="@+id/edit_test2" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" 28 android:layout_marginTop="50dp" 29 android:background="#eeeeee" 30 android:focusable="true" 31 android:hint="字体1" /> 32 33 <Button 34 android:id="@+id/button2" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:layout_marginRight="39dp" 38 android:layout_marginTop="14dp" 39 android:text="字体2" /> 40 41 </LinearLayout>
Java代码如下:
MainActivity.java
1 package com.example.edittest; 2 3 import android.app.Activity; 4 import android.graphics.Typeface; 5 import android.os.Bundle; 6 import android.view.KeyEvent; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.EditText; 11 import android.widget.TextView; 12 import android.widget.TextView.OnEditorActionListener; 13 import android.widget.Toast; 14 15 public class MainActivity extends Activity { 16 private EditText editText1; 17 private EditText editText2; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 initView(); 23 } 24 25 public void initView() { 26 editText1 = (EditText) findViewById(R.id.edit_test1); 27 editText2 = (EditText) findViewById(R.id.edit_test2); 28 29 //调用Typeface的createFromAsset方法 这里用两个ttf文件,ziti1.ttf、ziti2.ttf,这里用的是getAssets()这个方法 30 //字体1 路径为 assets目录下创建的fonts文件夹: /assets/fonts/ziti1.ttf 31 Typeface typeface1 = Typeface.createFromAsset(getAssets(), 32 "fonts/ziti1.ttf"); 33 editText1.setTypeface(typeface1); 34 35 // 监听回车键 36 editText1.setOnEditorActionListener(new OnEditorActionListener() { 37 @Override 38 public boolean onEditorAction(TextView v, int actionId, 39 KeyEvent event) { 40 Toast.makeText(MainActivity.this, String.valueOf(actionId), 41 Toast.LENGTH_SHORT).show(); 42 return false; 43 } 44 }); 45 //字体2 路径为 assets目录下创建的fonts文件夹: /assets/fonts/ziti2.ttf 46 Typeface typeface2 = Typeface.createFromAsset(getAssets(), 47 "fonts/ziti2.ttf"); 48 editText2.setTypeface(typeface2); 49 50 // 监听回车键 51 editText2.setOnEditorActionListener(new OnEditorActionListener() { 52 @Override 53 public boolean onEditorAction(TextView v, int actionId, 54 KeyEvent event) { 55 Toast.makeText(MainActivity.this, String.valueOf(actionId), 56 Toast.LENGTH_SHORT).show(); 57 return false; 58 } 59 }); 60 61 Button getValue1 = (Button) findViewById(R.id.button1); 62 Button getValue2 = (Button) findViewById(R.id.button2); 63 64 getValue1.setOnClickListener(new OnClickListener() { 65 @Override 66 public void onClick(View v) { 67 //Toast显示字符转换的Unicode码 68 Toast.makeText(MainActivity.this, 69 string2Unicode(editText1.getText().toString()), 70 Toast.LENGTH_SHORT).show(); 71 } 72 }); 73 getValue2.setOnClickListener(new OnClickListener() { 74 @Override 75 public void onClick(View v) { 76 //Toast显示字符转换的Unicode码 77 Toast.makeText(MainActivity.this, 78 string2Unicode(editText2.getText().toString()), 79 Toast.LENGTH_SHORT).show(); 80 } 81 }); 82 } 83 84 // 字符串转换成Unicode 85 public static String string2Unicode(String string) { 86 StringBuffer unicode = new StringBuffer(); 87 for (int i = 0; i < string.length(); i++) { 88 char c = string.charAt(i); 89 unicode.append("\\u" + Integer.toHexString(c)); 90 } 91 return unicode.toString(); 92 } 93 }
最后在edittext中输入字符后,点击button可分别显示出对应的Unicode码。
图和demo我都不上了,很简单的一个例子,希望能帮到需要的同学。
【原创】利用typeface实现不同字体的调用显示及String转换为Unicode
标签:
原文地址:http://www.cnblogs.com/raomengyang/p/4498688.html