Android EditText禁止输入中文字符

只允许EditText输入数字或者email格式字符是非常简单的(详见API文档)。 要实现只允许输入自定的数字字符也是很容易的(详见API文档,[setKeyListener(KeyListener)](http://blog.csdn.net/reference/android/widget/TextView.html#setKeyListener(android.text.method.KeyListener)) 和<span class="sympad">setFilters</span> <span class="normal">([InputFilter[]](http://blog.csdn.net/reference/android/text/InputFilter.html) filters)</span> )。 但是要限制只能输入指定的字母字符就非常的困难。 /** InputFilter[] ifs = {new InputFilter.LengthFilter(10),new DigitsKeyListener(true,true)}; editText01.setFilters(ifs); **/ &nbsp; 发现,**[Android](http://lib.csdn.net/base/15):digits**的参数是一个字符串,没有更多的语义检查,因此想到内部处理很可能仅是简单的检查,输入的字符是否在给定的字符串中(要我设计,我就这样做)。尝试着给它指定带有英文字符的参数,发现果然如此。万幸,万幸! 在layout.xml里 **[xml]** [view plain](http://blog.csdn.net/jdsjlzx/article/details/6762751#) [copy](http://blog.csdn.net/jdsjlzx/article/details/6762751#) <div> <embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" width="18" height="18" align="middle" name="ZeroClipboardMovie_1"> </embed> </div> </div> </div> - <span class="tag"><</span><span class="tag-name">EditText</span> - <span class="attribute">android:id</span>=<span class="attribute-value">&#8220;@+id/register_userName_editText&#8221;</span> - <span class="attribute">style</span>=<span class="attribute-value">&#8220;@style/register_editText_style&#8221;</span> - <span class="attribute">android:digits</span>=<span class="attribute-value">&#8220;@string/register_name_digits&#8221;</span> - <span class="attribute">android:textColor</span>=<span class="attribute-value">&#8220;@drawable/register_edit_text_textColor&#8221;</span> - <span class="attribute">android:textColorHint</span>=<span class="attribute-value">&#8220;@drawable/register_hint_text_textColor&#8221;</span> - <span class="attribute">android:hint</span>=<span class="attribute-value">&#8220;@string/register_name_hint_text&#8221;</span> - <span class="tag">/></span> &nbsp; 在string.xml里 &nbsp; **[xml]** [view plain](http://blog.csdn.net/jdsjlzx/article/details/6762751#) [copy](http://blog.csdn.net/jdsjlzx/article/details/6762751#) <div> <embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" width="18" height="18" align="middle" name="ZeroClipboardMovie_2"> </embed> </div> </div> </div> - <span class="tag"><</span><span class="tag-name">string</span> <span class="attribute">name</span>=<span class="attribute-value">&#8220;register_name_digits&#8221;</span><span class="tag">></span> - - ._0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLIMNOPQRSTUVWXYZ - - <span class="tag"></</span><span class="tag-name">string</span><span class="tag">></span> &nbsp; &nbsp; &nbsp; &nbsp; Java代码 - editText01.addTextChangedListener(<span class="keyword">new</span> TextWatcher() { - String tmp = <span class="string">&#8220;&#8221;</span>; - String digits = <span class="string">&#8220;abcdef&#8221;</span>; - <span class="annotation">@Override</span> - <span class="keyword">public</span> <span class="keyword">void</span> onTextChanged(CharSequence s, <span class="keyword">int</span> start, <span class="keyword">int</span> before, <span class="keyword">int</span> count) { - } - <span class="annotation">@Override</span> - <span class="keyword">public</span> <span class="keyword">void</span> beforeTextChanged(CharSequence s, <span class="keyword">int</span> start, <span class="keyword">int</span> count, <span class="keyword">int</span> after) { - tmp = s.toString(); - } - - <span class="annotation">@Override</span> - <span class="keyword">public</span> <span class="keyword">void</span> afterTextChanged(Editable s) { - Log.d(Sample4Main.TAG, <span class="string">&#8220;<><>afterTextChanged<><>&#8221;</span> + s.toString()); - - String str = s.toString(); - <span class="keyword">if</span>(str.equals(tmp)){ - <span class="keyword">return</span>; - } - - StringBuffer sb = <span class="keyword">new</span> StringBuffer(); - <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number"></span>; i < str.length(); i++){ - <span class="keyword">if</span>(digits.indexOf(str.charAt(i)) >= <span class="number"></span>){ - sb.append(str.charAt(i)); - } - } - tmp = sb.toString(); - editText01.setText(tmp); - } - });

2016年7月20日 · 2 分钟 · 天边的星星

【Android】自定义控件让TextView的drawableLeft与文本一起居中显示

前言 TextView的drawableLeft、drawableRight和drawableTop是一个常用、好用的属性,可以在文本的上下左右放置一个图片,而不使用更加复杂布局就能达到,我也常常喜欢用RadioButton的这几个属性实现很多效果,但是苦于不支持让drawbleLeft与文本一起居中,设置gravity为center也无济于事,终于有空研究了一下,这里与大家一起分享。 声明 欢迎转载,请注明出处! 博客园:http://www.cnblogs.com/ 农民伯伯: http://www.cnblogs.com/over140/ 正文 一、效果图 二、实现代码 自定义控件 ![复制代码](http://common.cnblogs.com/images/copycode.gif) /** * drawableLeft与文本一起居中显示 * * @author 农民伯伯 * @see http://www.cnblogs.com/over140/p/3464348.html * */ public class DrawableCenterTextView extends TextView { public DrawableCenterTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public DrawableCenterTextView(Context context, AttributeSet attrs) { super(context, attrs); } public DrawableCenterTextView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { Drawable[] drawables = getCompoundDrawables(); if (drawables != null) { Drawable drawableLeft = drawables[0]; if (drawableLeft != null) { float textWidth = getPaint().measureText(getText().toString()); int drawablePadding = getCompoundDrawablePadding(); int drawableWidth = 0; drawableWidth = drawableLeft.getIntrinsicWidth(); float bodyWidth = textWidth + drawableWidth + drawablePadding; } } super.onDraw(canvas); } } ...

2016年7月19日 · 1 分钟 · 天边的星星

Android share绘制虚线在手机上显示实线问题

可以说这是一个Bug, 据说在4.0以上机器会出现,我测试是android 4.4.2 Xml代码 <embed src="http://wv1124.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" type="application/x-shockwave-flash" width="14" height="15"> </embed> <a title="收藏这段代码">![收藏代码](http://wv1124.iteye.com/images/icon_star.png)</a> </div> - <span class="tag"><?</span><span class="tag-name">xml</span> <span class="attribute">version</span>=<span class="attribute-value">&#8220;1.0&#8221;</span> <span class="attribute">encoding</span>=<span class="attribute-value">&#8220;utf-8&#8221;</span><span class="tag">?></span> - <span class="tag"><</span><span class="tag-name">shape</span> <span class="attribute">xmlns:android</span>=<span class="attribute-value">&#8220;http://schemas.android.com/apk/res/android&#8221;</span> - <span class="attribute">android:shape</span>=<span class="attribute-value">&#8220;line&#8221;</span> <span class="tag">></span> - - <span class="tag"><</span><span class="tag-name">stroke</span> - <span class="attribute">android:dashGap</span>=<span class="attribute-value">&#8220;3dp&#8221;</span> - <span class="attribute">android:dashWidth</span>=<span class="attribute-value">&#8220;8dp&#8221;</span> - <span class="attribute">android:width</span>=<span class="attribute-value">&#8220;1dp&#8221;</span> - <span class="attribute">android:color</span>=<span class="attribute-value">&#8220;#999999&#8221;</span> <span class="tag">/></span> - - <span class="tag"><</span><span class="tag-name">size</span> <span class="attribute">android:height</span>=<span class="attribute-value">&#8220;1dp&#8221;</span> <span class="tag">/></span> - - <span class="tag"></</span><span class="tag-name">shape</span><span class="tag">></span> layout中引用: ...

2016年7月19日 · 2 分钟 · 天边的星星

charles工具抓包教程(http跟https)

1.下载charles 可以去charles官网下载,下载地址:http://www.charlesproxy.com/download/ 根据自己的操作系统下载对应的版本,然后进行安装,然后打开charles工具 2.设置代理(记住手机跟电脑要在同一个网络,用的同一个路由器) 1).查看默认端口 Proxy->Proxy Settings 在这个页面会看到HTTP Proxy的默认端口是8888 我们不需要修改,只需要知道有这个值就行 2).查看当前电脑的ip 例如我这里的是:192.168.9.129 3).知道了默认端口跟ip地址,我们就可以手机上设置代理了。设置步骤我录制了gif动画。(我用的是genymotion模拟器,真机也是一样的) 完成了以上两个步骤就可以抓到http请求的数据了,效果图如下: 接下来讲解如何抓取https协议的包,如果你没有这个需求,请不要继续浏览下面的内容 3.设置charles ssl代理 Proxy->SSL Proxy Settings 弹出一个ssl代理设置界面 1).Enable SSL Proxying复选框打上勾 2).添加你想要的设置代理的域名,端口默认443 设置过后效果图如下: 4.手机下载ssl证书 1).Help->SSL Proxying ->Install Charles Root Certificate on a Mobile Device or Remote Browser… 会弹出一个提示框,如下显示: 浏览器输入这个地址即可下载证书,记住要用Android自带的浏览器. 地址是:http://charlesproxy.com/getssl 扫一扫二维码安装ssl 2).然后按照他的提示安装证书即可. 做完第三步跟第四步就可以抓包https数据了.如果还有问题请留言。。。下面是我抓包截图。 方法二 HTTP包: 1.安装抓包工具 Charles , 到官网http://www.charlesproxy.com/可下载到最新版本 2.用安装了charles的电脑,代理待抓包anroid手机的网络连接: 首先查看pc的网络IP地址;打开手机设置,进入当前wifi连接,设置代理为手动,将服务器填为上一步中获得的IP,端口默认为8888(在charles的proxy setting中可以改这个端口号)。这时Charles弹出确认框,点击Allow按钮即可 3.中文乱码问题解决 在charles的content/info.plist 中 的vmoption 添加-Dfile.encoding=UTF-8 HTTPS抓包: 下载Charles证书http://www.charlesproxy.com/ssl.zip,解压后导入到手机中,然后设置->安全->凭据存储->从存储设备安装,选中证书。 在Charles的工具栏上点击设置按钮,选择Proxy Settings;切换到SSL选项卡,选中Enable SSL Proxying,选项卡的Locations表单可以填写要抓包的域名和端口,点击Add按钮,在弹出的表单中Host填写域名,比如填api.instagram.com,Port填443。默认的..表示应用于所有地址

2016年7月19日 · 1 分钟 · 天边的星星

InputStream与String,Byte之间互转

本文将介绍InputStream与String,Byte之间的相互转换。以代码来说明: **[html]** [view plain](http://blog.csdn.net/cjjky/article/details/6892443#) [copy](http://blog.csdn.net/cjjky/article/details/6892443#) <div> <embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" width="18" height="18" align="middle" name="ZeroClipboardMovie_1"> </embed> </div> </div> - import java.io.ByteArrayInputStream; - import java.io.ByteArrayOutputStream; - import java.io.IOException; - import java.io.InputStream; - - /** - * - * @author Andy.Chen - * @mail Chenjunjun.ZJ@gmail.com - * - */ - public class InputStreamUtils { - - final static int <span class="attribute">BUFFER_SIZE</span> = <span class="attribute-value">4096</span>; - - /** - * 将InputStream转换成String - * @param in InputStream - * @return String - * @throws Exception - * - */ - public static String InputStreamTOString(InputStream in) throws Exception{ - - ByteArrayOutputStream <span class="attribute">outStream</span> = <span class="attribute-value">new</span> ByteArrayOutputStream(); - byte[] <span class="attribute">data</span> = <span class="attribute-value">new</span> byte[BUFFER_SIZE]; - int <span class="attribute">count</span> = -1; - while((<span class="attribute">count</span> = <span class="attribute-value">in</span>.read(data,0,BUFFER_SIZE)) != -1) - outStream.write(data, 0, count); - - <span class="attribute">data</span> = <span class="attribute-value">null</span>; - return new String(outStream.toByteArray(),&#8221;ISO-8859-1&#8243;); - } - - /** - * 将InputStream转换成某种字符编码的String - * @param in - * @param encoding - * @return - * @throws Exception - */ - public static String InputStreamTOString(InputStream in,String encoding) throws Exception{ - - ByteArrayOutputStream <span class="attribute">outStream</span> = <span class="attribute-value">new</span> ByteArrayOutputStream(); - byte[] <span class="attribute">data</span> = <span class="attribute-value">new</span> byte[BUFFER_SIZE]; - int <span class="attribute">count</span> = -1; - while((<span class="attribute">count</span> = <span class="attribute-value">in</span>.read(data,0,BUFFER_SIZE)) != -1) - outStream.write(data, 0, count); - - <span class="attribute">data</span> = <span class="attribute-value">null</span>; - return new String(outStream.toByteArray(),&#8221;ISO-8859-1&#8243;); - } - - /** - * 将String转换成InputStream - * @param in - * @return - * @throws Exception - */ - public static InputStream StringTOInputStream(String in) throws Exception{ - - ByteArrayInputStream <span class="attribute">is</span> = <span class="attribute-value">new</span> ByteArrayInputStream(in.getBytes(&#8220;ISO-8859-1&#8221;)); - return is; - } - - /** - * 将InputStream转换成byte数组 - * @param in InputStream - * @return byte[] - * @throws IOException - */ - public static byte[] InputStreamTOByte(InputStream in) throws IOException{ - - ByteArrayOutputStream <span class="attribute">outStream</span> = <span class="attribute-value">new</span> ByteArrayOutputStream(); - byte[] <span class="attribute">data</span> = <span class="attribute-value">new</span> byte[BUFFER_SIZE]; - int <span class="attribute">count</span> = -1; - while((<span class="attribute">count</span> = <span class="attribute-value">in</span>.read(data,0,BUFFER_SIZE)) != -1) - outStream.write(data, 0, count); - - <span class="attribute">data</span> = <span class="attribute-value">null</span>; - return outStream.toByteArray(); - } - - /** - * 将byte数组转换成InputStream - * @param in - * @return - * @throws Exception - */ - public static InputStream byteTOInputStream(byte[] in) throws Exception{ - - ByteArrayInputStream <span class="attribute">is</span> = <span class="attribute-value">new</span> ByteArrayInputStream(in); - return is; - } - - /** - * 将byte数组转换成String - * @param in - * @return - * @throws Exception - */ - public static String byteTOString(byte[] in) throws Exception{ - - InputStream <span class="attribute">is</span> = <span class="attribute-value">byteTOInputStream</span>(in); - return InputStreamTOString(is); - } - - }

2016年7月18日 · 2 分钟 · 天边的星星

Android禁止ViewPager的左右滑动

有时候在开发中会遇到一些“诡异”的要求,比如在ViewPager中嵌入ListView,或者再嵌入一个ViewPager,那么在滑动的时候就会造成被嵌入的XXView不能滑动了,那么现在就把最外层的ViewPager禁止滑动吧,让被嵌入的XXView获得滑动事件好了。关于解决方法,网上也有很多说法,基本上是一致的,但是需要理解这个[Android](http://lib.csdn.net/base/15)下的事件分发机制才行,不明白事件分发机制的,上网查些资料看看,然后我这里也有简单的介绍,请参看博客Android自定义控件——侧滑菜单的下方。 怎样禁止ViewPager左右滑动呢?大致就是重写ViewPager,覆盖ViewPager的onInterceptTouchEvent(MotionEvent arg0)方法和onTouchEvent(MotionEvent arg0)方法,这两个方法的返回值都是boolean类型的,只需要将返回值改为false,那么ViewPager就不会消耗掉手指滑动的事件了,转而传递给上层View去处理或者该事件就直接终止了。下面是我的自定义ViewPager。 ``` `<span class=“hljs-keyword”>public</span> <span class=“hljs-class”><span class=“hljs-keyword”>class</span> <span class=“hljs-title”>NoScrollViewPager</span> <span class=“hljs-keyword”>extends</span> <span class=“hljs-title”>ViewPager</span> {</span> <span class=“hljs-keyword”>private</span> <span class=“hljs-keyword”>boolean</span> noScroll = <span class=“hljs-keyword”>false</span>; &lt;span class="hljs-keyword">public&lt;/span> &lt;span class="hljs-title">NoScrollViewPager&lt;/span>(Context context, AttributeSet attrs) { &lt;span class="hljs-keyword">super&lt;/span>(context, attrs); &lt;span class="hljs-comment">// TODO Auto-generated constructor stub&lt;/span> } &lt;span class="hljs-keyword">public&lt;/span> &lt;span class="hljs-title">NoScrollViewPager&lt;/span>(Context context) { &lt;span class="hljs-keyword">super&lt;/span>(context); } &lt;span class="hljs-keyword">public&lt;/span> &lt;span class="hljs-keyword">void&lt;/span> &lt;span class="hljs-title">setNoScroll&lt;/span>(&lt;span class="hljs-keyword">boolean&lt;/span> noScroll) { &lt;span class="hljs-keyword">this&lt;/span>.noScroll = noScroll; } &lt;span class="hljs-annotation">@Override&lt;/span> &lt;span class="hljs-keyword">public&lt;/span> &lt;span class="hljs-keyword">void&lt;/span> &lt;span class="hljs-title">scrollTo&lt;/span>(&lt;span class="hljs-keyword">int&lt;/span> x, &lt;span class="hljs-keyword">int&lt;/span> y) { &lt;span class="hljs-keyword">super&lt;/span>.scrollTo(x, y); } &lt;span class="hljs-annotation">@Override&lt;/span> &lt;span class="hljs-keyword">public&lt;/span> &lt;span class="hljs-keyword">boolean&lt;/span> &lt;span class="hljs-title">onTouchEvent&lt;/span>(MotionEvent arg0) { &lt;span class="hljs-comment">/* return false;//super.onTouchEvent(arg0); */&lt;/span> &lt;span class="hljs-keyword">if&lt;/span> (noScroll) &lt;span class="hljs-keyword">return&lt;/span> &lt;span class="hljs-keyword">false&lt;/span>; &lt;span class="hljs-keyword">else&lt;/span> &lt;span class="hljs-keyword">return&lt;/span> &lt;span class="hljs-keyword">super&lt;/span>.onTouchEvent(arg0); } &lt;span class="hljs-annotation">@Override&lt;/span> &lt;span class="hljs-keyword">public&lt;/span> &lt;span class="hljs-keyword">boolean&lt;/span> &lt;span class="hljs-title">onInterceptTouchEvent&lt;/span>(MotionEvent arg0) { &lt;span class="hljs-keyword">if&lt;/span> (noScroll) &lt;span class="hljs-keyword">return&lt;/span> &lt;span class="hljs-keyword">false&lt;/span>; &lt;span class="hljs-keyword">else&lt;/span> &lt;span class="hljs-keyword">return&lt;/span> &lt;span class="hljs-keyword">super&lt;/span>.onInterceptTouchEvent(arg0); } &lt;span class="hljs-annotation">@Override&lt;/span> &lt;span class="hljs-keyword">public&lt;/span> &lt;span class="hljs-keyword">void&lt;/span> &lt;span class="hljs-title">setCurrentItem&lt;/span>(&lt;span class="hljs-keyword">int&lt;/span> item, &lt;span class="hljs-keyword">boolean&lt;/span> smoothScroll) { &lt;span class="hljs-keyword">super&lt;/span>.setCurrentItem(item, smoothScroll); } &lt;span class="hljs-annotation">@Override&lt;/span> &lt;span class="hljs-keyword">public&lt;/span> &lt;span class="hljs-keyword">void&lt;/span> &lt;span class="hljs-title">setCurrentItem&lt;/span>(&lt;span class="hljs-keyword">int&lt;/span> item) { &lt;span class="hljs-keyword">super&lt;/span>.setCurrentItem(item); } }` ...

2016年7月15日 · 1 分钟 · 天边的星星

Android下如何计算要显示的字符串所占的宽度和高度

查询了google发现在android一下有几种方法可以做到,但是经过实际测试发现下面这种方法是最准确的 Rect bounds = new Rect(); String text = "Hello World"; TextPaint paint; paint = findViewById(R.id.hello_world).getPaint(); paint.getTextBounds(text, 0, text.length(), bounds); int width = bounds.width(); ``` Paint pFont = new Paint(); Rect rect = new Rect(); pFont.getTextBounds("豆", 0, 1, rect); Log.v(TAG, "height:"+rect.height()+"width:"+rect.width()); ## Android_FontMetrics对象的各种基准线(以及怎么获取文字的width和height) </div> Canvas 作为绘制文本时,使用FontMetrics对象,计算位置的坐标。 public static class FontMetrics { /** * The maximum distance above the baseline for the tallest glyph in * the font at a given text size. / public float top; /* * The recommended distance above the baseline for singled spaced text. / public float ascent; /* * The recommended distance below the baseline for singled spaced text. / public float descent; /* * The maximum distance below the baseline for the lowest glyph in * the font at a given text size. / public float bottom; /* * The recommended additional space to add between lines of text. */ public float leading; } ...

2016年7月12日 · 2 分钟 · 天边的星星

WebSocket心跳实现

//间隔发送心跳包数据给服务器,服务器在一定时间内发回心跳包响应,对比超时限定,如果超过设定的超时时间,则认为当前与服务器的websocket连接已经断开,关闭当前web socket连接,善后处理,例如重新连接,或者弹出提示…… function keepalive(ws) { var time = new Date(); //连接断开,可设置重连或者关闭连接 (“#keeplive_box”).html(“服务器没有响应.”).css({ “color” : “red” }); //ws.close(); } else {(“#keeplive_box”).html(“连接正常”).css({ “color” : “green” }); if (ws.bufferedAmount == 0) { ws.send(‘H#C’); } } } var ws = new WebSocket(to_url); ws.onopen = function () { (“#statustxt”).html(“connected.”);(“#send_btn”).attr(“disabled”, false); heartbeat_timer = setInterval(function () { keepalive(ws) }, 3000); }

2016年7月4日 · 1 分钟 · 天边的星星

MVC模式优缺点

优点: 耦合性低 重用性高 生命周期成本低 部署快 可维护性高 有利软件工程化管理 缺点: 没有明确的定义 不适合小型、中等规模 增加系统结构和实现的复杂性 视图与控制器间过于紧密的连接 视图对模型数据的低效率访问 一般高级的界面工具或构造器不支持模式

2016年6月27日 · 1 分钟 · 天边的星星

android SDK 19以上代码设置状态栏透明

//判断android 版本然后设置Systembar颜色-设置透明 public void initSystemBar() { Window window = getWindow(); //4.4版本及以上 if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.KITKAT) { window.setFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } //5.0版本及以上 if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(Color.TRANSPARENT); } }

2016年6月22日 · 1 分钟 · 天边的星星