移动开发人机交互

一、密码长度太短了提示

第一种方法:

final EditText editText= (EditText) findViewById(R.id.editText);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
	@Override
    public void onFocusChange(View v, boolean hasFocus) {
        // TODO Auto-generated method stub

        if (!hasFocus) { //如果失去焦点
        	EditText eText = (EditText) v;
            int len = eText.length();
            if (len < 5)="" {="" etext.settext("");//清空="" toast.maketext(testlinearlayout.this,="" "长度短了!len="+len, Toast.LENGTH_SHORT).show();
            } else {
            	Toast.makeText(TestLinearLayout.this, " 长度符合!len="+len, Toast.LENGTH_SHORT).show();
            }
        }
    }
});

Button button = (Button) findViewById(R.id.editTextb);
button.setOnClickListener(new OnClickListener() {
	@Override
    public void onClick(View v) {
    	editText.clearFocus(); //失去焦点
    }
});

第二种方法:

在确定的监听中取得输入框“输入字符的长度”len,在这里判断,如果len<4就不跳转,然后showtoast.提示就可以了,符合要求再正常做。


二、Android实现长时间不用屏幕变暗

原理就是修改window的亮度,然后达到让屏幕变黑的效果,通过监听activity的dispatchTouchEvent方法来全局监听屏幕的变化。

import android.app.Activity;  
import android.os.Bundle;  
import android.os.Handler;  
import android.os.Looper;  
import android.view.MotionEvent;  
import android.view.WindowManager;  
  
public class BaseActivity extends Activity {  
  
    /** 
     * 最大的屏幕亮度 
     */  
    float maxLight;  
    /** 
     * 当前的亮度 
     */  
    float currentLight;  
  
    /** 
     * 用来控制屏幕亮度 
     */  
    Handler handler;  
  
    /** 
     * 延时时间 
     */  
    long DenyTime = 5 * 1000L;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        InitData();  
    }  
  
    private void InitData() {  
        handler = new Handler(Looper.getMainLooper());  
        maxLight = GetLightness(this);  
    }  
  
    /** 
     * 设置亮度 
     *  
     * @param context 
     * @param light 
     */  
    void SetLight(Activity context, int light) {  
        currentLight = light;  
        WindowManager.LayoutParams localLayoutParams = context.getWindow().getAttributes();  
        localLayoutParams.screenBrightness = (light / 255.0F);  
        context.getWindow().setAttributes(localLayoutParams);  
    }  
  
    /** 
     * 获取亮度 
     *  
     * @param context 
     * @return 
     */  
    float GetLightness(Activity context) {  
        WindowManager.LayoutParams localLayoutParams = context.getWindow().getAttributes();  
        float light = localLayoutParams.screenBrightness;  
        return light;  
    }  
  
    @Override  
    protected void onPause() {  
        super.onPause();  
        stopSleepTask();  
    }  
  
    @Override  
    protected void onResume() {  
        super.onResume();  
        startSleepTask();  
    }  
  
    @Override  
    public boolean dispatchTouchEvent(MotionEvent ev) {  
        if (currentLight == 1) {  
            startSleepTask();  
        }  
        return super.dispatchTouchEvent(ev);  
    }  
  
    /** 
     * 开启休眠任务 
     */  
    void startSleepTask() {  
        SetLight(this, (int) maxLight);  
        handler.removeCallbacks(sleepWindowTask);  
        handler.postDelayed(sleepWindowTask, DenyTime);  
    }  
  
    /** 
     * 结束休眠任务 
     */  
    void stopSleepTask() {  
        handler.removeCallbacks(sleepWindowTask);  
    }  
  
    /** 
     * 休眠任务 
     */  
    Runnable sleepWindowTask = new Runnable() {  
  
        @Override  
        public void run() {  
            SetLight(BaseActivity.this, 1);  
        }  
    };  
  
}  


三、android监听返回按钮事件

@Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    } 
    protected void dialog() { 
        AlertDialog.Builder builder = new Builder(HanderTestActivty.this); 
        builder.setMessage("确定要退出吗?"); 
        builder.setTitle("提示"); 
        builder.setPositiveButton("确认", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        HanderTestActivty.this.finish(); 
                    } 
                }); 
        builder.setNegativeButton("取消", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                    } 
                }); 
        builder.create().show(); 
    } 
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
            dialog(); 
            return false; 
        } 
        return false; 
    }


四、Android利用Bundle实现Activity间消息的传递

关于Activity之间的消息的传递。利用Bundle是一种比较方便的办法。 程序的效果是活动A向B跳转的同时发送一字符串,B读出字符串。 先在AndroidManifest.xml中定义一个新的Activity

1.[XML]代码     


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<? xml version = "1.0" encoding = "utf-8" ?>
< manifest xmlns:android = "http://schemas.android.com/apk/res/android"
       package = "com.ray.test"
       android:versionCode = "1"
       android:versionName = "1.0" >
     < application android:icon = "@drawable/icon" android:label = "@string/app_name" >
         < activity android:name = ".TestBundle"
                   android:label = "@string/app_name" >
             < intent-filter >
                 < action android:name = "android.intent.action.MAIN" />
                 < category android:name = "android.intent.category.LAUNCHER" />
             </ intent-filter >
         </ activity >
           
         < activity android:name = ".Target" ></ activity >
     </ application >
     < uses-sdk android:minSdkVersion = "3" />
</ manifest >

2. TestBundle.java     


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.ray.test; 
  
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.MotionEvent; 
  
public class TestBundle extends Activity { 
     public void onCreate(Bundle savedInstanceState) { 
         super .onCreate(savedInstanceState); 
         setContentView(R.layout.main); 
    
       
     public boolean onTouchEvent(MotionEvent event) { 
         Intent intent = new Intent(); 
         intent.setClass(TestBundle. this , Target. class ); 
         Bundle mBundle = new Bundle(); 
         mBundle.putString( "Data" , "ray'blog" ); //压入数据 
          intent.putExtras(mBundle); 
         startActivity(intent); 
         finish(); 
         return super .onTouchEvent(event); 
     }
}

3. TargetActivity.java     


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.ray.test; 
 
import android.app.Activity; 
import android.os.Bundle; 
  
public class TargetActivity extends Activity{ 
       
     public void onCreate(Bundle savedInstanceState) { 
         super .onCreate(savedInstanceState); 
         setContentView(R.layout.main); 
         Bundle bundle = getIntent().getExtras();   
         String data=bundle.getString( "Data" ); //读出数据 
         setTitle(data); 
    
}

五、还可改变屏幕方向来使人机交互性更好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值