最近工作用到,就写个小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我都不上了,很简单的一个例子,希望能帮到需要的同学。

【原创】利用typeface实现不同字体的调用显示及String转换为Unicode的更多相关文章

  1. 利用ManualResetEvent来来控制异步调用的打印的线程的暂停和恢复(转)

    利用ManualResetEvent来来控制异步调用的打印的线程的暂停和恢复 打印过程可能很长,这时候有可能需要暂停下来做一些事情,然后回来继续接着打印 打印过程中有2个线程:一个是程序运行的主线程, ...

  2. python利用or在列表解析中调用多个函数.py

    python利用or在列表解析中调用多个函数.py """ python利用or在列表解析中调用多个函数.py 2016年3月15日 05:08:42 codegay & ...

  3. 利用JS实现闪烁字体

    以下为在JSP文件中,利用JS实现闪烁字体的代码: HTML代码: <div id="blink">一段会闪烁的字</div> JavaScript代码: ...

  4. SpringCloud系列-利用Feign实现声明式服务调用

    上一篇文章<手把手带你利用Ribbon实现客户端的负载均衡>介绍了消费者通过Ribbon调用服务实现负载均衡的过程,里面所需要的参数需要在请求的URL中进行拼接,但是参数太多会导致拼接字符 ...

  5. JavaScript网站设计实践(四)编写about.html页面,利用JavaScript和DOM,选择性的显示和隐藏DIV元素

    一.现在我们在网站设计(三)的基础上,来编写about.html页面. 这个页面要用到的知识点是利用JavaScript和DOM实现选择性地显示和隐藏某些DIV about.html页面在前面我们为了 ...

  6. 利用动态图层实现数据的实时显示(arcEngine IDynamiclayer)

    marine 原文利用动态图层实现数据的实时显示(arcEngine IDynamiclayer) 说明:最近一个项目用到这方面知识,文章主要来至网络,后期会加入自己的开发心得.(以下的代码实例中,地 ...

  7. android如何调用显示和隐藏系统默认的输入法(一)

    1.调用显示系统默认的输入法 方法一. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_MET ...

  8. C# 利用反射动态创建对象——带参数的构造函数和String类型

    C# 利用反射动态创建对象——带参数的构造函数和String类型 最近笔者有一个想法需要利用反射动态创建对象(如string,int,float,bool,以及自定义类等)来实现,一直感觉反射用不好, ...

  9. 利用Python计算π的值,并显示进度条

    利用Python计算π的值,并显示进度条  第一步:下载tqdm 第二步;编写代码 from math import * from tqdm import tqdm from time import ...

随机推荐

  1. "+" 是怎样连接字符串的?

    关于“+”运算符对字符串的连接,不同的平台在实现上可能会略有不同. 1. Oracle JDK1.7 当使用“+”对字符串进行连接时,会创建一个临时的StringBuilder对象,该对象调用appe ...

  2. 对部分Java名词的总结

    对部分Java名词的总结: 前端技术: CSS:是层叠样式表(Cascading Style Sheets)的缩写,它用于定义HTML元素的显示形式,是W3C推出的格式化网页内容的标准技术. Boot ...

  3. 黑马程序员+ADO.Net基础(中)

    ---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net ...

  4. 快速学习C语言二: 编译自动化, 静态分析, 单元测试,coredump调试,性能剖析

    上次的Hello world算是入门了,现在学习一些相关工具的使用 编译自动化 写好程序,首先要编译,就用gcc就好了,基本用法如下 gcc helloworld.c -o helloworld.o ...

  5. Android度量单位说明(DIP,DP,PX,SP) (转帖)

    (一)概念 dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不 ...

  6. 软将工程课设day9

    UI设计demo2.0. 在昨日demo的基础上进行了优化. 撰写美工设计报告,个人报告

  7. MQTT V3.1----flow

    该文章转自:聂永的博客(http://www.blogjava.net/yongboy/archive/2014/02/15/409893.html) 网络故障 在任何网络环境下,都会出现一方连接失败 ...

  8. 我的ORM之三 -- 更新

    我的ORM索引 更新语法 var 影响行数 = dbr.表.Update(实体).Where(条件).Execute(); 实体类型: 更新的实体类型和添加的实体类型一样,有三类: 1. 任何C#类. ...

  9. Sql Server对象管理器的使用

    VS提供了很多便捷的工具,Sql Server对象管理器可以直接在VS里面访问数据库,不用再打开一个Management Studio.这里记录下Sql Server对象管理器的使用. 1.先在视图里 ...

  10. Git学习笔记(1)——安装,配置,创建库,文件添加到库

    初次接触git,为了记忆深刻,把学习的简单流程记录下来. 本文记录了Git在Ubuntu上的安装,配置,以及创建版本库和往库中添加文件的过程. 1.Git的安装:(Ubuntu-Linux非常友好的安 ...