移动开发人机交互

来源:互联网 发布:js数组删除元素splice 编辑:程序博客网 时间:2024/06/08 14:23

一、密码长度太短了提示

第一种方法:

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
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
      package="com.ray.test"
      android:versionCode="1"
      android:versionName="1.0">
    <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
        <activityandroid:name=".TestBundle"
                  android:label="@string/app_name">
            <intent-filter>
                <actionandroid:name="android.intent.action.MAIN"/>
                <categoryandroid:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
           
        <activityandroid:name=".Target"></activity>
    </application>
    <uses-sdkandroid: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
packagecom.ray.test; 
  
importandroid.app.Activity; 
importandroid.content.Intent; 
importandroid.os.Bundle; 
importandroid.view.MotionEvent; 
  
publicclassTestBundle extendsActivity { 
    publicvoidonCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    
       
    publicbooleanonTouchEvent(MotionEvent event) { 
        Intent intent = newIntent(); 
        intent.setClass(TestBundle.this, Target.class); 
        Bundle mBundle = newBundle(); 
        mBundle.putString("Data","ray'blog");//压入数据 
         intent.putExtras(mBundle); 
        startActivity(intent); 
        finish(); 
        returnsuper.onTouchEvent(event); 
    }
}

3. TargetActivity.java     


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
packagecom.ray.test; 
 
importandroid.app.Activity; 
importandroid.os.Bundle; 
  
publicclassTargetActivity extendsActivity{ 
       
    publicvoidonCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        Bundle bundle = getIntent().getExtras();   
        String data=bundle.getString("Data");//读出数据 
        setTitle(data); 
    
}

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

0 0
原创粉丝点击