1.Android EditText的输入监听,输入字符的动态获取
有时候我们可能会用到时时的监听EditText输入字符的时时监听,监听字符的个数,做一些正则表达式的处理等。如下方法可以实现:
我做的是时时的把EditeText输入的数据同步到TextView上
布局文件:
清单文件中需要加入两个权限:
MainActivity实现:
PowerManager这个类提供了电源管理的一些功能,比如可以让屏幕或者键盘亮起来等。还有对设备的重启的api
官网是这么解释的
这个类提供了控制设备电源状态的管理功能。
设备的电池的持续时间(寿命)会受到使用这个API的重要影响。在非必要的情况下不要使用WakeLock,即使必须使用,也要最低限度使用这个api,使用完之后应确保立即释放掉。
通过 Context.getSystemService()获得PowerManager的实例。
使用PowerManager的实例去获得一个WakeLock对象,使用这个方法:newWakeLock() 创建一个
PowerManager.WakeLock
对象.使用WakeLock对象可以去管理设备电源的状态,使用方法特别简单:如下例子
<span class="typ" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 0, 102);">PowerManager</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);"> pm </span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">=</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);"> </span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">(</span><span class="typ" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 0, 102);">PowerManager</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">)</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);"> getSystemService</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">(</span><span class="typ" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 0, 102);">Context</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">.</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);">POWER_SERVICE</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">);</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);"> </span><span class="typ" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 0, 102);">PowerManager</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">.</span><span class="typ" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 0, 102);">WakeLock</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);"> wl </span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">=</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);"> pm</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">.</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);">newWakeLock</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">(</span><span class="typ" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 0, 102);">PowerManager</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">.</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);">SCREEN_DIM_WAKE_LOCK</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">,</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);"> </span><span class="str" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 136, 0);">"My Tag"</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">);</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);"> wl</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">.</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);">acquire</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">();</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);"> //在释放之前,屏幕一直亮着(有可能会变暗,但是还可以看到屏幕内容)</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);"> wl</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">.</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);">release</span><span class="pun" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(102, 102, 0);">();</span><span class="pln" style="padding: 0px; margin: 0px; line-height: 25.200000762939453px; color: rgb(0, 0, 0);"> </span>
下面定义的这些标记不同程度的影响系统电源。这些标记都是独占的,并且每次只能指定其中一个。
Flag Value | CPU | Screen | Keyboard |
---|---|---|---|
PARTIAL_WAKE_LOCK |
On* | Off(关闭) | Off(关闭) |
SCREEN_DIM_WAKE_LOCK |
On | Dim(变暗) | Off(关闭) |
SCREEN_BRIGHT_WAKE_LOCK |
On | Bright(高亮) | Off(关闭) |
FULL_WAKE_LOCK |
On | Bright(高亮) | Bright(高亮) |
如果你使用的是局部唤醒锁的话(使用PARTIAL_WAKE_LOCK标志),CPU会继续运行,将忽略任何的计时器,甚至按下电源按钮。其他的唤醒锁话,CPU也会继续运转,但是使用者仍然可以按电源按钮让设备睡眠。
另外,你可以使用两个以上的标记,但是他只影响屏幕的行为。和 PARTIAL_WAKE_LOCK 同时使用的话,没有任何影响。
Flag Value | Description |
---|---|
ACQUIRE_CAUSES_WAKEUP |
Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.正常情况下是不会是屏幕等变亮,相反,当获得wakeLock之后需要一个触发事件才会使屏幕或者键盘变亮。典型应用是一个对用户来说比较重要的通知时,使用这个锁。 |
ON_AFTER_RELEASE |
If this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.帮忙翻译一下吧 |
一个小例子
package com.example.powers;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
private WakeLock wakeLock;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
// 开始获得唤醒锁
acquireWakeLock();
}
@Override
public void finish() {
super.finish();
// 释放锁
releaseWakeLock();
}
private void acquireWakeLock() {
if (wakeLock == null) {
Log.d("debug", "Acquiring wake lock");
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, this
.getClass().getCanonicalName());
wakeLock.acquire();
}
}
private void releaseWakeLock() {
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
wakeLock = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
http协议上传文件一般最大是2M,比较适合上传小于两M的文件
下面是封装的一个文件类:
在清单文件中加上网络访问权限
<!-- 访问internet权限 -->
<uses-permissionandroid:name="android.permission.INTERNET"/>
利用HttpURLConnection对象,我们可以从网络中获取网页数据.
URL url = newURL("http://www.sohu.com");
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5*1000);//设置连接超时
conn.setRequestMethod(“GET”);//以get方式发起请求
if(conn.getResponseCode() != 200) throw new RuntimeException("请求url失败");
InputStream is =conn.getInputStream();//得到网络返回的输入流
Stringresult = readData(is, "GBK");
conn.disconnect();
//第一个参数为输入流,第二个参数为字符集编码
public static StringreadData(InputStream inSream, String charsetName) throws Exception{
ByteArrayOutputStreamoutStream = new ByteArrayOutputStream();
byte[]buffer = new byte[1024];
intlen = -1;
while((len = inSream.read(buffer)) != -1 ){
outStream.write(buffer,0, len);
}
byte[]data = outStream.toByteArray();
outStream.close();
inSream.close();
returnnew String(data, charsetName);
}
利用HttpURLConnection对象,我们可以从网络中获取文件数据.
URL url = newURL("http://photocdn.sohu.com/20100125/Img269812337.jpg");
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5*1000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() != 200) throw new RuntimeException("请求url失败");
InputStream is =conn.getInputStream();
readAsFile(is,"Img269812337.jpg");
public static voidreadAsFile(InputStream inSream, File file) throws Exception{
FileOutputStreamoutStream = new FileOutputStream(file);
byte[]buffer = new byte[1024];
intlen = -1;
while((len = inSream.read(buffer)) != -1 ){
outStream.write(buffer,0, len);
}
outStream.close();
inSream.close();
}
Android中Button 有focused, selected, pressed 等不同状态,通过配置一个XML格式的 drawable "selector" 即可实现”在不同状态下显示不同背景图片“的功能。 1. 在res/drawable目录下添加一个xml文件,用来描述Button在不同状态下对应的不同图片。我这里给该xml文件命名为btn_background.xml <?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/btn_pressed" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/btn_normal" /> <!-- focused --> <item android:drawable="@drawable/btn_normal" /> <!-- default --> </selector> 2. 在res/layout目录下,对应的layout xml文件中,将Button的android:background属性设置为btn_background即可。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/btn_background" /> </LinearLayout> 3.运行结果 默认状态(unselected) 点击状态(pressed) |