Android知识点记录(2)

来源:互联网 发布:无人机用什么语言编程 编辑:程序博客网 时间:2024/05/21 02:37

1  ViewAnimator的使用

    ViewAnimator继承来至 FrameLayout. 布局如下:

    <ViewAnimator        android:id="@+id/viewanimator"        android:layout_width="wrap_content"        android:layout_height="wrap_content" >        <FrameLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal|bottom"            android:layout_marginTop="90px" >            <Button                android:id="@+id/recover_button"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_gravity="center_horizontal|bottom"                android:layout_marginTop="75px"                android:background="@drawable/common_button_state"                android:text="@string/hello_world"                android:textColor="@android:color/white"                android:textSize="30px" />        </FrameLayout>        <FrameLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal|bottom"            android:layout_marginTop="90px" >            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_gravity="center"                android:src="@drawable/check_progress_background" />            <ImageView                android:id="@+id/recover_progress"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_gravity="center"                android:src="@drawable/check_image" />            <TextView                android:id="@+id/recover_text"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_gravity="center_horizontal|bottom"                android:layout_marginTop="80px"                android:text="检测状态..."                android:textColor="#ffffff"                android:textSize="24px" />        </FrameLayout>    </ViewAnimator>
     Activity中使用如下:

ViewAnimator animator = (ViewAnimator) findViewById(R.id.viewanimator);        animator.setDisplayedChild(0);ButtonrecoverButton = (Button) animator.findViewById(R.id.recover_button);ImageView progressBar = (ImageView) animator.findViewById(R.id.recover_progress);TextView  recoverText = (TextView) animator.findViewById(R.id.recover_text);
        animator.setDisplayedChild(1);
2   sqlite时间函数使用

     时间戳是以毫秒为单位,需要除以1000转化为秒

     http://www.sqlite.org/lang_datefunc.html

     查询今天之内的记录

  date(implementation_date/1000,'unixepoch','localtime')==date('now','localtime')
3  sqlite数据库升级,增加字段

   1  可以直接使用sqlite语句升级

private static final int VER_LAUNCH = 21;private static final int VER_SESSION_FEEDBACK_URL = 22;private static final int VER_SESSION_NOTES_URL_SLUG = 23;private static final int DATABASE_VERSION = VER_SESSION_NOTES_URL_SLUG;@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    Log.d(TAG, "onUpgrade() from " + oldVersion + " to " + newVersion);    // NOTE: This switch statement is designed to handle cascading database    // updates, starting at the current version and falling through to all    // future upgrade cases. Only use "break;" when you want to drop and    // recreate the entire database.    int version = oldVersion;    switch (version) {        case VER_LAUNCH:            // Version 22 added column for session feedback URL.            db.execSQL("ALTER TABLE " + Tables.SESSIONS + " ADD COLUMN "                    + SessionsColumns.SESSION_FEEDBACK_URL + " TEXT");            version = VER_SESSION_FEEDBACK_URL;        case VER_SESSION_FEEDBACK_URL:            // Version 23 added columns for session official notes URL and slug.            db.execSQL("ALTER TABLE " + Tables.SESSIONS + " ADD COLUMN "                    + SessionsColumns.SESSION_NOTES_URL + " TEXT");            db.execSQL("ALTER TABLE " + Tables.SESSIONS + " ADD COLUMN "                    + SessionsColumns.SESSION_SLUG + " TEXT");            version = VER_SESSION_NOTES_URL_SLUG;    }    Log.d(TAG, "after upgrade logic, at version " + version);    if (version != DATABASE_VERSION) {        Log.w(TAG, "Destroying old data during upgrade");        db.execSQL("DROP TABLE IF EXISTS " + Tables.BLOCKS);        // ... delete all your tables ...        onCreate(db);     } }
  2  使用备份表

  So what is recommended onUpgrade:

  • beginTransaction
  • run a table creation with if not exists (we are doing an upgrade, so the table might not exists yet, it will fail alter and drop)
  • put in a list the existing columns List<String> columns = DBUtils.GetColumns(db, TableName);
  • backup table (ALTER table " + TableName + " RENAME TO 'temp_" + TableName)
  • create new table (the newest table creation schema)
  • get the intersection with the new columns, this time columns taken from the upgraded table (columns.retainAll(DBUtils.GetColumns(db, TableName));)
  • restore data (String cols = StringUtils.join(columns, ","); db.execSQL(String.format( "INSERT INTO %s (%s) SELECT %s from temp_%s", TableName, cols, cols, TableName));)
  • remove backup table (DROP table 'temp_" + TableName)
  • setTransactionSuccessful

   附加:SQLite目前还不支持drop column

   4   左右滑动手势监听

   public class LeftAndRightGestureDetector {/** * 滑动的间隙,当滑动的开始位置和结束位置之间 的距离不小于该值时,这个类才认为是一次滑动事件 */private final int DISTANCE = 150;private SimpleGestureListener simpleGestureListener;/** * 注册监听 *  * @param view *            需要监听的控件 * @param listener *            手势监听器监听器 */public void registerListener(View view, SimpleGestureListener listener) {this.simpleGestureListener = listener;final GestureDetector gestureDetector = new GestureDetector(gestureListener);view.setLongClickable(true);view.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {return gestureDetector.onTouchEvent(event);}});}/** * 手势监听 */private OnGestureListener gestureListener = new OnGestureListener() {/** * up事件 */@Overridepublic boolean onSingleTapUp(MotionEvent e) {return false;}@Overridepublic void onShowPress(MotionEvent e) {}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) {return false;}@Overridepublic void onLongPress(MotionEvent e) {}/** * 左右滑动事件 MotionEvent e1 可能为null 所以做容错处理 */@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {if (simpleGestureListener != null) {if (e1 != null && e2 != null) {if (e1.getX() - e2.getX() > DISTANCE&& Math.abs(velocityX) > 0) {simpleGestureListener.onLeftFling();} else if (e2.getX() - e1.getX() > DISTANCE&& Math.abs(velocityX) > 0) {simpleGestureListener.onRightFling();}}}return false;}/** * Down事件 */@Overridepublic boolean onDown(MotionEvent e) {return false;}};public interface SimpleGestureListener {/** * 当手势向左滑时触发 */void onLeftFling();/** * 当手势向右滑时触发 */void onRightFling();}}

5  防止快速点击Button,多次响应

  public abstract class OnSingleClickListener implements View.OnClickListener {private static final long MIN_CLICK_INTERVAL = 600;private long mLastClickTime;public abstract void onSingleClick(View v);@Overridepublic final void onClick(View v) {long currentClickTime = SystemClock.uptimeMillis();long elapsedTime = currentClickTime - mLastClickTime;// 有可能2次连击,也有可能3连击,保证mLastClickTime记录的总是上次click的时间mLastClickTime = currentClickTime;if (elapsedTime <= MIN_CLICK_INTERVAL) {return;}onSingleClick(v);}}
6 广播监听网络是否发生变化

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {checkAndSetNetworkState();}};protected void onStart() {super.onStart();//广播监听网络是否发生变化IntentFilter iff = new IntentFilter();iff.addAction("android.net.conn.CONNECTIVITY_CHANGE");registerReceiver(mBroadcastReceiver, iff);}@Overridepublic void onPause() {super.onPause();unregisterReceiver(mBroadcastReceiver);}

7 利用RadioGroup实现Tab切换效果

 <RadioGroup    android:id="@+id/rdogrp"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_margin="8dp"    android:gravity="center"    android:orientation="horizontal" >    <RadioButton        android:id="@+id/btn1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="0dp"        android:background="@drawable/btn1Selector"        android:button="@null"        android:checked="true"        android:gravity="center" />    <RadioButton        android:id="@+id/btn2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="0dp"        android:background="@drawable/btn2Selector"        android:button="@null"        android:gravity="center" />    <RadioButton        android:id="@+id/btn3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="0dp"        android:background="@drawable/btn3Selector"        android:button="@null"        android:gravity="center" />    <RadioButton        android:id="@+id/btn4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="0dp"        android:background="@drawable/btn4Selector"        android:button="@null"        android:gravity="center" /></RadioGroup>
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/btn1_selected" android:state_checked="true"  />    <item android:drawable="@drawable/btn1_normal" android:state_checked="false"/></selector>

android:state_pressed 是否按下,如一个按钮触摸或者点击。
android:state_focused 是否取得焦点,比如用户选择了一个文本框。
android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected 被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。
android:state_checkable 组件是否能被check。如:RadioButton是可以被check的。
android:state_checked 被checked了,如:一个RadioButton可以被check了。
android:state_enabled 能够接受触摸或者点击事件
android:state_activated 被激活(这个麻烦举个例子,不是特明白)
android:state_window_focused 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台

注意:如果有多个item,那么程序将自动从上到下进行匹配,最先匹配的将得到应用。(不是通过最佳匹配)
如果一个item没有任何的状态说明,那么它将可以被任何一个状态匹配。

8  Popupwindow弹窗外不可进行按键响应

View view = LayoutInflater.from(TractionAnglePromptActivity.this).inflate(R.layout.popup, null);final PopupWindow popup = new PopupWindow(view);<span style="font-family:Arial, Helvetica, sans-serif;">popup</span>.setWindowLayoutMode(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);popup.setBackgroundDrawable(new BitmapDrawable());popup.setTouchable(true);popup.setFocusable(false);popup.setOutsideTouchable(false);
很多弹窗都是非全屏,但R.layout.popup的宽高必须都设置为fill_parent,否则不起作用

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@android:color/transparent" >    ......</FrameLayout>


  

















































0 0
原创粉丝点击