【原创】利用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 ...
随机推荐
- UNET学习笔记1 - 总览
UNET为两类人设计: (1)使用Unity开发简单多人在线游戏.这类用户可以从NetworkManager或者the High Level API开始: (2)开发复杂多人在线游戏或者开发网络服务. ...
- 作业七:团队项目——Alpha版本冲刺阶段-08
昨天进展:代码编写. 今天安排:代码编写.
- C#最良心脚本语言C#Light/Evil,Xamarin\WP8\Unity热更新最良心方案,再次进化.
C#Light的定位是嵌入式脚本语言,一段C#Light脚本是一个函数 C#Evil定位为书写项目的脚本语言,多脚本文件合作,可以完全用脚本承载项目. C#Light/Evil 使用完全C#一致性语法 ...
- 不插网线,看不到IP的解决办法
在Windows中,如果不插网线,就看不到IP地址,即使这个块网卡已经绑定了固定IP,原因是操作系统开启了DHCP Media Sense功能,该功能的作用如下: 在一台使用 TCP/IP 的基于 W ...
- [安卓] 19、一个蓝牙4.0安卓DEMO
一.工程结构&概述 整个应用包含两个按钮,一个是搜索所有蓝牙设备,另一个是连接所有蓝牙设备~ 下面是整个工程的文件结构: 二.代码业务流程跟踪 2.1.两个按钮 下面讲讲该工程里的主要跳转流程 ...
- 达洛克战记3 即将开服! What's New!
历经数个月的开发,达洛克战记3即将全新开服! 剧情: 回归到三大种族起源时期,三大种族并没有像现在三足鼎立.人类一直处于统治地位.但是突然间一群巨人的出现,让人类损失惨重,身为勇者,需要探索巨人背后的 ...
- 在ubuntu上安装nodejs[开启实时web时代]
作为一名菜鸟,竟然在centos桌面上连输入命令行的地方都找不到,是在是对不起开山祖师,最后苍天不负苦心人,在ubuntu上找见了 [安装过程参考了http://cnodejs.org/topic/4 ...
- [全文检索]Lucene基础入门.
本打算直接来学习Solr, 现在先把Lucene的只是捋一遍. 本文内容: 1. 搜索引擎的发展史 2. Lucene入门 3. Lucene的API详解 4. 索引调优 5. Lucene搜索结果排 ...
- Atitit html5 Canvas 如何自适应屏幕大小
Atitit html5 Canvas 如何自适应屏幕大小 可以用JS监控屏幕大小,然后调整Canvas的大小.在代码中加入JS 1 2 3 4 5 6 7 $(window).resize ...
- Eclipse中java向数据库中添加数据,更新数据,删除数据
前面详细写过如何连接数据库的具体操作,下面介绍向数据库中添加数据. 注意事项:如果参考下面代码,需要 改包名,数据库名,数据库账号,密码,和数据表(数据表里面的信息) package com.ning ...