【原创】利用typeface实现不同字体的调用显示及String转换为Unicode
最近工作用到,就写个小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的更多相关文章
- 利用ManualResetEvent来来控制异步调用的打印的线程的暂停和恢复(转)
利用ManualResetEvent来来控制异步调用的打印的线程的暂停和恢复 打印过程可能很长,这时候有可能需要暂停下来做一些事情,然后回来继续接着打印 打印过程中有2个线程:一个是程序运行的主线程, ...
- python利用or在列表解析中调用多个函数.py
python利用or在列表解析中调用多个函数.py """ python利用or在列表解析中调用多个函数.py 2016年3月15日 05:08:42 codegay & ...
- 利用JS实现闪烁字体
以下为在JSP文件中,利用JS实现闪烁字体的代码: HTML代码: <div id="blink">一段会闪烁的字</div> JavaScript代码: ...
- SpringCloud系列-利用Feign实现声明式服务调用
上一篇文章<手把手带你利用Ribbon实现客户端的负载均衡>介绍了消费者通过Ribbon调用服务实现负载均衡的过程,里面所需要的参数需要在请求的URL中进行拼接,但是参数太多会导致拼接字符 ...
- JavaScript网站设计实践(四)编写about.html页面,利用JavaScript和DOM,选择性的显示和隐藏DIV元素
一.现在我们在网站设计(三)的基础上,来编写about.html页面. 这个页面要用到的知识点是利用JavaScript和DOM实现选择性地显示和隐藏某些DIV about.html页面在前面我们为了 ...
- 利用动态图层实现数据的实时显示(arcEngine IDynamiclayer)
marine 原文利用动态图层实现数据的实时显示(arcEngine IDynamiclayer) 说明:最近一个项目用到这方面知识,文章主要来至网络,后期会加入自己的开发心得.(以下的代码实例中,地 ...
- android如何调用显示和隐藏系统默认的输入法(一)
1.调用显示系统默认的输入法 方法一. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_MET ...
- C# 利用反射动态创建对象——带参数的构造函数和String类型
C# 利用反射动态创建对象——带参数的构造函数和String类型 最近笔者有一个想法需要利用反射动态创建对象(如string,int,float,bool,以及自定义类等)来实现,一直感觉反射用不好, ...
- 利用Python计算π的值,并显示进度条
利用Python计算π的值,并显示进度条 第一步:下载tqdm 第二步;编写代码 from math import * from tqdm import tqdm from time import ...
随机推荐
- 多线程_先产后销_运行结果有BUG
class Shop { public static void main(String[] args) { Things t=new Things(); Custom c=new Custom(t); ...
- haproxy的使用
假如 www.example.com想要使用haproxy作为代理,则要在自己的 dns服务器设置AAAA记录对应于haproxy机器的IP. 这样访问www.example.com其实就访问了hap ...
- markdown语法记录
换行:在结尾加两个空格后回车. 缩进:将输入法切换到全角,两个空格就是两个汉字的大小.
- Hasor-Core v0.0.4 & Web v0.0.3 发布
“Hasor是一款开源的 Java 应用开发框架.它是围绕 Guice 为核心创建的一系列模块组合而成.使用Hasor 会加快软件软件开发效率并降低开发成本.目前 Hasor包含了 Hasor-Cor ...
- Doc转文本
微软方案VC 原文:http://www.cnblogs.com/eaglet/archive/2013/01/10/2854224.html 从 http://ifilter.codeplex.co ...
- 异步CTP(Async CTP)为什么那样工作?
对异步CTP感兴趣有很多原因.异步CTP使异步编程比以前更加容易了.它虽然没有Rx强大,但是更容易学.异步CTP介绍了两个新的关键字,async和await.异步方法(或Lambda表达式)必须返回v ...
- ReentrantLock实现原理深入探究
前言 这篇文章被归到Java基础分类中,其实真的一点都不基础.网上写ReentrantLock的使用.ReentrantLock和synchronized的区别的文章很多,研究ReentrantLoc ...
- Linux 数组
200 ? "200px" : this.width)!important;} --> :介绍 在shell4.0之后支持普通数组和关联数组,普通数组只能使用整数作为索引,关 ...
- ios UIView sizeToFit sizeThatFits
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 0, 0)]; testLabel.backgroundC ...
- Java对象序列化---转载
1.概念 序列化:把Java对象转换为字节序列的过程. 反序列化:把字节序列恢复为Java对象的过程. 2.用途 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通常存放在一个 ...