【原创】利用typeface实现不同字体的调用显示及String转换为Unicode
最近工作用到,就写个小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我都不上了,很简单的一个例子,希望能帮到需要的同学。
转载于:https://www.cnblogs.com/raomengyang/p/4498688.html
【原创】利用typeface实现不同字体的调用显示及String转换为Unicode相关推荐
- DL之NN:利用(本地数据集50000张数据集)调用自定义神经网络network.py实现手写数字图片识别94%准确率
DL之NN:利用(本地数据集50000张数据集)调用自定义神经网络network.py实现手写数字图片识别94%准确率 目录 输出结果 代码设计 输出结果 更新-- 代码设计 import mnist ...
- html 载入服务器字体,css调用服务器端字体示例代码
css 怎么调用外部字体 在test页面打开的页面中添加一个class为anim的div代码.如下图所示: 在打开的页面找到接着设置an 比如我网页设置 微软雅黑 字体在我的电脑上安装了雅黑字体肯定能 ...
- [原创]利用橡皮条技术画图
[原创]利用橡皮条技术画图 在进行有绘制图形功能的软件开发中都会面临橡皮条技术,所谓橡皮条技术就是在绘制过程中出现鼠标移动的记录,当鼠标松开时绘制完整的图形.下面给出一个简单的橡皮条技术的应用,该应用 ...
- html5中字体显示不出来怎么办,手机字体无法正常显示怎么办
手机字体无法正常显示怎么办 步骤1.首先获取手机root权限.上网搜索并下载"z4Root"程序,该程序的功能是获取手机Root权限. 步骤2.利用数据线将手机连接到电脑USB接口 ...
- Android 屏幕适配(修改系统字体大小、显示大小、修改分辨率相关适配)
Android实际开发中会遇到各种各样的机型,所以就需要对多种机型进行适配.常见的适配在这类就不详细介绍了,大家可以参考这里的系列文章Android通用屏幕适配讲解的已经很详细了.下面说的是:当修改系 ...
- 【第3版emWin教程】第32章 emWin6.x的矢量字体(支持汉字全字库,Unicode编码,QSPI Flash方案)
教程不断更新中:http://www.armbbs.cn/forum.php?mod=viewthread&tid=98429 第32章 emWin6.x的矢量字体(支持汉字全字库 ...
- BootStrap字体图标不显示、下拉菜单不显示
在W3CSchool学习BootStrap教程时遇到的问题-- 1.字体图标不能显示 上面是正常显示的样子,不能正常显示,因为缺少下面的字体图标样式文件 (1)在下载的Bootstrap包中将font ...
- html在线随机抽取,利用纯html+javascript实现随机抽取显示
求助:利用纯html+javascript实现随机抽取显示 1 一个智力问答页面,每次显示一个判断题,最多可以连续答10题.题目从固定的10个问题中随机抽出一个且不能重复. 2 问题只有两种答案,对或 ...
- Chrome 对于 glyphicon 字体图标不显示的解决办法
在将Chome默认字体渲染为微软雅黑后,部分字体图标显示为方框,这里Chome扩展文档提供的解决办法为: 找到 custom.css 文件,路径为: C:\Users\(用户名)\AppData\L ...
最新文章
- 状态估计问题学习记录(1)
- 网站ui的配色有哪些需要注意的?
- 了解SQL和MySQL数据库
- amd为什么还用针脚_为什么AMD不取消cpu上的针脚?
- md5与des算法有何不同_到底AI芯片和传统芯片有何区别?
- 谷歌翻译 google translation api
- python中if控制语句_Python 极简教程(十二)逻辑控制语句 if else
- C/C++网络编程工作笔记0003---客户服务端程序说明
- Heartbeat+DRBD+MySQL高可用架构方案与实施过程细节
- atitit.浏览器插件解决方案----ftp插件 attilax 总结
- 【NOIP2015】运输计划
- Linux 重启oracle数据库
- OSError: [Errno 8] Exec format error
- 药品计算机系统操作知识培训,新版GSP:计算机系统专业知识培训测试题(6)
- Vivado中FIFO遇到【Common17-55】警告总结
- 上周内外盘行情回顾及交易情况2022.4.4
- 【盒模型】什么是盒模型
- 通俗理解OvO与OvR
- Zabbx6.0(学习笔记)
- CAD关于图层删除图层操作(com接口c#语言)