Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 開發隨筆:時間控件、數據庫、字符串及相關邏輯

開發隨筆:時間控件、數據庫、字符串及相關邏輯

編輯:關於Android編程

開發隨筆,小結項目開發中的得與失,要點如下:

進展-推薦功能及相關頁面:
1.相關控件的使用:TimePicker、RadioGroup、RadioButton、Button的隱藏;

時間選擇控件TimePicker,

 

A view for selecting the time of day, ineither 24 hour or AM/PM mode. The hour, each minute digit, and AM/PM (ifapplicable) can be conrolled by vertical spinners. The hour can be entered bykeyboard input. Entering in two digit hours can be accomplished by hitting twodigits within a timeout of about a second (e.g. '1' then '2' to select 12). Theminutes can be entered by entering single digits. Under AM/PM mode, the usercan hit 'a', 'A", 'p' or 'P' to pick.

最好的學習材料,官方文檔:http://www.android-doc.com/reference/android/widget/TimePicker.html

常見方法包括:setIs24HourView設置24小時制、setOnTimeChangedListener實時監聽最新時間。

        startTime_tp_suggest.setIs24HourView(true);
        startTime_tp_suggest.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hour, int minute) {
                mStartHour = hour;
                mStartMinute = minute;
            }
        });

RadioGroup和RadioButton,RadioGroup設置監聽事件setOnCheckedChangeListener,參數int id對應RadioButton的id,進行相應操作。
lou_rg_suggest = (RadioGroup) findViewById(R.id.lou_rg_suggest);
        lou_rg_suggest.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int id) {
                switch (id){
                    case R.id.e11_rb_suggest:
                        Toast.makeText(SuggestActivity.this, "E11", Toast.LENGTH_SHORT).show();
                        mLou = "E11";
                        break;
                    case R.id.e13_rb_suggest:
                        Toast.makeText(SuggestActivity.this, "E13", Toast.LENGTH_SHORT).show();
                        mLou = "E13";
                        break;
                    default:
                        break;
                }
            }
        });

 

Button的隱藏,很簡單,在xml文件中添加:

android:visibility="invisible"

 

需要時只需要調用相關方法顯示即可。

xxx.setVisibility(View.VISIBLE);

 

相關背景知識:

android view的setVisibility方法值有3個:
1.View.VISIBLE,常量值為0,意思是可見的
2.View.INVISIBLE,常量值是4,意思是不可見的
3.View.GONE,常量值是8,意思是不可見的,並且不占用布局空間。

View.class官方源代碼:

    public static final int VISIBLE = 0;
    public static final int INVISIBLE = 4;
    public static final int GONE = 8;

 

2.推薦邏輯,樓層的匹配,時間安排分情況討論;

以安排會議情況為例,畫了一個示意圖,簡單梳理邏輯:

\

 

3.數據庫查詢query()函數參數,多游標的使用以及判斷:

 

                Cursor cursor = db.query("Room", null, null, null, null, null,
                        "name,time");
末尾參數ORDER BY 語句用於根據指定的列對結果集進行排序,可以多關鍵字,像本條語句先按name後按time排序。

 

 

Cursor:

 

This interface provides random read-writeaccess to the result set returned by a database query.

Cursor implementations are not required tobe synchronized so code using a Cursor from multiple threads should perform itsown synchronization when using the Cursor.

最好的學習材料,官方文檔:http://www.android-doc.com/reference/android/database/Cursor.html

多游標的使用,類似多指針,可以跟著走,也可以隨意走,妙用多多哈,數據庫和游標用完一定要關閉,重要的事情說一遍。

 

                Cursor cursorNext = cursor;
                //public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
                if (cursor.moveToFirst() && cursorNext.moveToFirst()) {
                    do {
                                cursorNext.moveToPosition(cursor.getPosition() + 1);

                        }
                    } while (cursor.moveToNext());
                }
                cursor.close();
                db.close();

 


4.字符串的處理:分割,比較,格式化;

經常變化的字符串用StringBuffer和它的append方法,提升效率,

 

                time_tv_suggest.setText(new StringBuffer().append("  ").append(FormatTime(mStartHour))
                        .append(":").append(FormatTime(mStartMinute))
                        .append("-").append(FormatTime(mEndHour))
                        .append(":").append(FormatTime(mEndMinute)));

split分割操作,comparto比較大小

 

 

                    do {
                        String local = cursor.getString(cursor
                                .getColumnIndex("local"));
                        String time = cursor.getString(cursor
                                .getColumnIndex("time"));
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                         Log.d("權興權意-Cursor", "name-" + name + " time-" + time + " local-" + local);
                        String localArray[] = local.split("-");
                        String timeArray[] = time.split("-");
                        if(localArray[0].equals(mLou) && localArray[1].equals(mCeng)){
                            mStartTime = mStartHour + ":" + mStartMinute;
                            mEndTime = mEndHour + ":" + mEndMinute;
                            //區間比較-APP時間位於會議兩組時間之間
                            if(mStartTime.compareTo(timeArray[1]) > 0){
                                cursorNext.moveToPosition(cursor.getPosition() + 1);
                                if(name.equals(cursorNext.getString(cursor.getColumnIndex("name")))){
                                    String timeNext = cursorNext.getString(cursor
                                            .getColumnIndex("time"));
                                    String timeArrayNext[] = timeNext.split("-");
                                    if(timeArrayNext[0].compareTo(mEndTime) > 0){
                                        roomName = name;
                                        break;
                                    }
                                }else{
                                    //一個房間只有一個會議,會議晚於最晚時間
                                    roomName = name;
                                    break;
                                }
                            }
                        }
                    } while (cursor.moveToNext());

有時顯示時間如果是一位感覺很奇怪,類似於“9:1”和“09:01”,顯然我們習慣於看到後者,手寫一個處理函數吧:

 

 

    private String FormatTime(int time){
        String s = Integer.toString(time);
        if(s.length() == 1){
            s = "0" + s;
        }
        return s;
    }


 

 

文末小彩蛋:

 

android studio怎麼查看類中已有的方法:

查詢本類裡面所有的方式快捷鍵:Ctrl+o (需要把AS的快捷鍵設置成Eclipse)

ctrl查看源碼必備操作,至於怎麼把AS快捷鍵設置成Eclipse,請看上一遍博客哈。

 

小結:

 

進展-推薦功能及相關頁面:
1.相關控件的使用:TimePicker、RadioGroup、RadioButton、Button的隱藏;
2.推薦邏輯,樓層的匹配,時間安排分情況討論;
3.數據庫查詢query()函數參數,多游標的使用以及判斷;
4.字符串的處理:分割,比較,格式化;

 

附上核心源碼,教學相長,歡迎交流。

 

package com.quan.car.qmeeting;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import dbutils.MyDatabaseHelper;

/**
 * Created by 權興權意 on 2016/8/23.
 */
public class SuggestActivity extends Activity{

    private TimePicker startTime_tp_suggest;
    private TimePicker endTime_tp_suggest;
    private TextView time_tv_suggest;
    private Button yesTime_btn_suggest;
    private int mStartHour;
    private int mStartMinute;
    private int mEndHour;
    private int mEndMinute;
    private String mStartTime;
    private String mEndTime;

    private RadioGroup lou_rg_suggest;
    private RadioGroup ceng_rg_suggest;
    private TextView floor_tv_suggest;
    private Button yesFloor_btn_suggest;
    private String mLou;
    private String mCeng;

    private Button suggest_btn_suggest;
    private MyDatabaseHelper dbHelper;
    private String roomName = "Room1";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.suggest);

        dbHelper = new MyDatabaseHelper(this, "Meeting.db", null, MyDatabaseHelper.DB_VERSION);

        time_tv_suggest = (TextView) findViewById(R.id.time_tv_suggest);
        startTime_tp_suggest = (TimePicker) findViewById(R.id.startTime_tp_suggest);
        startTime_tp_suggest.setIs24HourView(true);
        startTime_tp_suggest.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hour, int minute) {
                mStartHour = hour;
                mStartMinute = minute;
            }
        });
        endTime_tp_suggest = (TimePicker) findViewById(R.id.endTime_tp_suggest);
        endTime_tp_suggest.setIs24HourView(true);
        endTime_tp_suggest.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hour, int minute) {
                mEndHour = hour;
                mEndMinute = minute;
            }
        });
        yesTime_btn_suggest = (Button) findViewById(R.id.yesTime_btn_suggest);
        yesTime_btn_suggest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                time_tv_suggest.setText(new StringBuffer().append("  ").append(FormatTime(mStartHour))
                        .append(":").append(FormatTime(mStartMinute))
                        .append("-").append(FormatTime(mEndHour))
                        .append(":").append(FormatTime(mEndMinute)));
            }
        });

        floor_tv_suggest = (TextView) findViewById(R.id.floor_tv_suggest);
        lou_rg_suggest = (RadioGroup) findViewById(R.id.lou_rg_suggest);
        lou_rg_suggest.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int id) {
                switch (id){
                    case R.id.e11_rb_suggest:
                        Toast.makeText(SuggestActivity.this, "E11", Toast.LENGTH_SHORT).show();
                        mLou = "E11";
                        break;
                    case R.id.e13_rb_suggest:
                        Toast.makeText(SuggestActivity.this, "E13", Toast.LENGTH_SHORT).show();
                        mLou = "E13";
                        break;
                    default:
                        break;
                }
            }
        });
        ceng_rg_suggest = (RadioGroup) findViewById(R.id.ceng_rg_suggest);
        ceng_rg_suggest.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int id) {
                switch (id){
                    case R.id.ceng1_rb_suggest:
                        Toast.makeText(SuggestActivity.this, "1", Toast.LENGTH_SHORT).show();
                        mCeng = "1";
                        break;
                    case R.id.ceng2_rb_suggest:
                        Toast.makeText(SuggestActivity.this, "2", Toast.LENGTH_SHORT).show();
                        mCeng = "2";
                        break;
                    case R.id.ceng3_rb_suggest:
                        Toast.makeText(SuggestActivity.this, "3", Toast.LENGTH_SHORT).show();
                        mCeng = "3";
                        break;
                    case R.id.ceng4_rb_suggest:
                        Toast.makeText(SuggestActivity.this, "4", Toast.LENGTH_SHORT).show();
                        mCeng = "4";
                        break;
                    case R.id.ceng5_rb_suggest:
                        Toast.makeText(SuggestActivity.this, "5", Toast.LENGTH_SHORT).show();
                        mCeng = "5";
                        break;
                    default:
                        break;
                }
            }
        });
        yesFloor_btn_suggest = (Button) findViewById(R.id.yesFloor_btn_suggest);
        yesFloor_btn_suggest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                floor_tv_suggest.setText(new StringBuffer().append("  ").append((mLou))
                        .append("-").append(mCeng));
            }
        });

        suggest_btn_suggest = (Button) findViewById(R.id.suggest_btn_suggest);
        suggest_btn_suggest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor cursor = db.query("Room", null, null, null, null, null,
                        "name,time");
                Cursor cursorNext = cursor;
                //public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
                if (cursor.moveToFirst() && cursorNext.moveToFirst()) {
                    do {
                        String local = cursor.getString(cursor
                                .getColumnIndex("local"));
                        String time = cursor.getString(cursor
                                .getColumnIndex("time"));
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                         Log.d("權興權意-Cursor", "name-" + name + " time-" + time + " local-" + local);
                        String localArray[] = local.split("-");
                        String timeArray[] = time.split("-");
                        if(localArray[0].equals(mLou) && localArray[1].equals(mCeng)){

                            //1.會議室為空,無會議,直接預訂;
                            if (time.equals("")){
                                roomName = name;
                                break;
                            }

                            mStartTime = mStartHour + ":" + mStartMinute;
                            mEndTime = mEndHour + ":" + mEndMinute;
                            //3.會議室有多場會議,遍歷操作,區間:
                            //將會議室會議根據時間按降序排列,取區間如圖,
                            //C的開始時間晚於A的結束時間,
                            //C的結束時間早於B的開始時間,
                            if(mStartTime.compareTo(timeArray[1]) > 0){
                                cursorNext.moveToPosition(cursor.getPosition() + 1);
                                if(name.equals(cursorNext.getString(cursor.getColumnIndex("name")))){
                                    String timeNext = cursorNext.getString(cursor
                                            .getColumnIndex("time"));
                                    String timeArrayNext[] = timeNext.split("-");
                                    if(timeArrayNext[0].compareTo(mEndTime) > 0){
                                        roomName = name;
                                        break;
                                    }
                                }else{
                                    //2.2預訂會議開始時間晚於原會議結束時間;
                                    roomName = name;
                                    break;
                                }
                            }

                            //2.會議室只有1場會議:
                            //2.1預訂會議結束時間早於原會議開始時間;
                            if(timeArray[0].compareTo(mEndTime) > 0)
                                roomName = name;
                            break;

                        }
//                        Log.d("權興權意:", "nameCur-" + nameCur);
//                        Log.d("權興權意:", "passwordCur-" + passwordCur);
//                        Log.d("權興權意:", "idCur-" + idCur);
                    } while (cursor.moveToNext());
                }
                cursor.close();
                db.close();

                Intent intent = new Intent();
                intent.setClass(SuggestActivity.this, RoomListActivity.class);
                intent.putExtra("roomName",roomName);
                startActivity(intent);
            }
        });
//        Toast.makeText(SuggestActivity.this, "SuggestActivity--" + mHour + ":" + mMinute,
//                Toast.LENGTH_SHORT).show();
    }

    private String FormatTime(int time){
        String s = Integer.toString(time);
        if(s.length() == 1){
            s = "0" + s;
        }
        return s;
    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved