2011年12月18日 星期日

android設計 listview讓使用者設定音樂鬧鐘(類似檔案總管)

 使用listview,先建立xml的TextView

 file_list.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  

</TextView>

使用 ArrayAdapter就能讓ListView列出List

 ArrayAdapter ad;
  ad=new ArrayAdapter(this,R.layout.file_list,CLOCK1_widget1_music_file.get_file_list(SD_PATH));
                
                list_view.setAdapter(ad);



 ListView事件傾聽(實做ListView.OnItemClickListener)讓選中list item回應事件
public class clockConfigure extends Activity implements RadioGroup.OnCheckedChangeListener,ListView.OnItemClickListener{...}

list_view=(ListView)findViewById(R.id.listView1);
               
               // list_view.setOnClickListener(this);
              //設定list_view傾聽事件
                list_view.setOnItemClickListener(this);

//實做(implements ListView.OnItemClickListener完後,在esclipe視窗中按右鍵source,Override/implements method會自動載入)
@Override



public void onItemClick(AdapterView arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            //上一層
            String st;
            String select;
            //當前檔案
            select=CLOCK1_widget1_music_file.get_file_path()+"/"+CLOCK1_widget1_music_file.get_file(arg2);
            //當前檔案
           
             Log.d("TAG new path file",select);
             Log.d("TAG  path ",CLOCK1_widget1_music_file.get_file_path());
           
             ad=new ArrayAdapter(this,R.layout.file_list,CLOCK1_widget1_music_file.get_file_list(select));
           
                list_view.setAdapter(ad);
           
           
        }





接下來是重點CLOCK1_widget1_music_file.java這個來處理檔案list,getParent()這函式可取得,當前目錄或file的PATH(譬如/mnt/sdcard,就得到/mnt,在一次就變/),判斷當給一個路徑是directory或要回上一層,傳回file list
在此使用 List來存放file list(他是一個增長型array),但一定要初始化get_file_path()和get_file(int i)這2個static方法也很重要,取得path和file,雖然看起來很簡單,但是邏輯很難(想很久才想出來),結果像影片中可前進後退,到根目錄會就不能回上一層,滿好的,現在我只是要讓使用者能設定音樂當鬧鐘,已經快完成了(待續,完成後整個程式會post上來)






package test.Clock_widget1;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class CLOCK1_widget1_music_file {
    private static File myFilePath;
    private static List dirarray;
    private static String filepath;
    public CLOCK1_widget1_music_file(String path) {
   
       
 
    }
    //給路徑,取得all file list,給目錄或回上一層使用
    public static List get_file_list(String path){
         myFilePath = new File(path);
         filepath=path;
   
        //必須初始化array,否則error
        dirarray=new ArrayList();
        //加上第一項
        dirarray.add("回到上一層");
       
   
       
         //是目錄回傳list
       
        if(myFilePath.isDirectory()){
         for(int j=0;j
             dirarray.add( myFilePath.list()[j]);
             //Log.d("TAG F:",dirarray[j]);
         }
   
                
       
        }else{
            //不是目錄回到上一層
            //先回到當前目錄
            filepath=myFilePath.getParent();
            File f=new File(filepath);
            //在一次就是上層目錄,但是若是根目錄除外
            if(!filepath.equals("/")){
            filepath=f.getParent();
            }
            myFilePath = new File(filepath );
           
           
            for(int j=0;j
                 dirarray.add( myFilePath.list()[j]);
                 //Log.d("TAG F:",dirarray[j]);
             }
           
        }
        return dirarray;
       
       
    }
    //當前路徑
    public static String get_file_path(){
       
        return filepath;
               
    }
    //選中file
    public static String get_file(int i){
       
        return dirarray.get(i);
               
    }
    public static boolean is_directory(String st){
        File myFilePath = new File(st);
        return myFilePath.isDirectory();
    }
    public static boolean is_file(String st){
        File myFilePath = new File(st);
        return myFilePath.isFile();
    }
   
}





read more...

2011年12月6日 星期二

android appwidget使用thread (更新)

 Clock_widget.java這個已經我改寫了,這才是我想要寫的thread ,不需要透過Handler,public static void start_service()這靜態方法讓thread直接呼叫來啟動service,

 thread中有3個靜態方法set_second(int s) ,stop_thread(),start_thread()讓使用者來設定顯示時間間隔,停止,開始時間,其實我這project已經多加了許多功能(譬如鬧鐘,顯示方
式..),但是還不是我想要的.

滿有趣的我用了一個反向寫程式方法來啟動音樂,在Clock_widget.java結尾的地方(藍色部份)

其他程式部份我會在放上來(待續)

終於知道寫法犯了錯誤,下面的 public void onReceive(Context context, Intent intent)會啟動public  Clock_widget()這個建構子,每當我按鈕,onReceive接收,就會呼叫public  Clock_widget()這個建構子,因此thread會在產生,一直按就會一直產生thread,解決方法public void onUpdate才是初始化的地方,將public  Clock_widget()拿掉,把內容放置onUpdate就OK了.

package test.Clock_widget1;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class CLOCK1_widget1_music_file {
    private static File myFilePath;
    private static List dirarray;
    private static String filepath;
    public CLOCK1_widget1_music_file(String path) {
  
    //必須初始化array,否則error
  
 
    }
    //給路徑,取得all file list,給目錄或回上一層使用
    public static List get_file_list(String path){
         myFilePath = new File(path);
         filepath=path;
  
        //必須初始化array,否則error
        dirarray=new ArrayList();
        //加上第一項
        dirarray.add("回到上一層");
      
  
      
         //是目錄回傳list
      
        if(myFilePath.isDirectory()){
         for(int j=0;j
             dirarray.add( myFilePath.list()[j]);
             //Log.d("TAG F:",dirarray[j]);
         }
  
               
      
        }else{
            //不是目錄回到上一層
            //先回到當前目錄
            filepath=myFilePath.getParent();
            File f=new File(filepath);
            //在一次就是上層目錄,但是若是根目錄除外
            if(!filepath.equals("/")){
            filepath=f.getParent();
            }
            myFilePath = new File(filepath );
          
          
            for(int j=0;j
                 dirarray.add( myFilePath.list()[j]);
                 //Log.d("TAG F:",dirarray[j]);
             }
          
        }
        return dirarray;
      
      
    }
    //當前路徑
    public static String get_file_path(){
      
        return filepath;
              
    }
    //選中file
    public static String get_file(int i){
      
        return dirarray.get(i);
              
    }
    public static boolean is_directory(String st){
        File myFilePath = new File(st);
        return myFilePath.isDirectory();
    }
    public static boolean is_file(String st){
        File myFilePath = new File(st);
        return myFilePath.isFile();
    }
  
}

read more...

2011年12月3日 星期六

TK的canvas元件,清除畫面

 畫了3個圓
按清除,被清空畫布

 使用set將圓形放入變數中
 set cir [$obj create oval $x0 $y0 $x1 $y1 -fill $color -width $line_width ]
 就可使用delete來清除了
 $obj delete $dc


範例:


#!/usr/bin/wish
#wm deiconify .
#wm title . "test"
#新建窗口
global circle
toplevel .main
wm title .main "Input String Field"
#指定窗口大小(400x400)和位置(60,60)
wm geometry .main 400x400+60+60; update
wm maxsize .main 1028 512
wm minsize .main 128 1
set ::c1 {}
set cx 100
set cy 100
set cr 50
#在窗口開一個frame
frame .main.frame1 -bd 2 -width 400 -height 300 -relief raised
place .main.frame1 -x 0 -y 0
#在frame建立畫布
canvas .main.frame1.canvas
place .main.frame1.canvas -x 0 -y 0
#在frame建立輸入框x
label .main.frame1.lx -text "X:"
place .main.frame1.lx -x 2 -y 270
entry .main.frame1.ex -width 5 -textvariable cx
place .main.frame1.ex -x 20 -y 270
#在frame建立輸入框y
label .main.frame1.ly -text "Y:"
place .main.frame1.ly -x 82 -y 270
entry .main.frame1.ey -width 5 -textvariable cy
place .main.frame1.ey -x 100 -y 270
#在frame建立輸入框r
label .main.frame1.lr -text "R:"
place .main.frame1.lr -x 162 -y 270
entry .main.frame1.er -width 5 -textvariable cr
place .main.frame1.er -x 180 -y 270
tk_optionMenu .main.frame1.to color red blue yellow green
place .main.frame1.to -x 260 -y 270
tk_optionMenu .main.frame1.tol line_width 1 2 3 4 5
place .main.frame1.tol -x 340 -y 270

#按鈕Draw
button .main.a -text "畫圓" -command { draw_circle .main.frame1.canvas  $cx $cy $cr $line_width $color }
place .main.a -x 20 -y 360
#按鈕Close
button .main.a1 -text "清除" -command {
delete_circle .main.frame1.canvas $::circle 

unset ::circle
#destroy .main 
}
place .main.a1 -x 100 -y 360
#按鈕Close
button .main.a2 -text "關閉" -command {
destroy .main
}

place .main.a2 -x 200 -y 360
proc draw_circle {obj x y r line_width color } {
global node
set x0 [ expr $x - $r ]
set y0 [ expr $y - $r ]
set x1 [ expr $x + $r ]
set y1 [ expr $y + $r ]
set cir [$obj create oval $x0 $y0 $x1 $y1 -fill $color -width $line_width ]
lappend ::circle $cir
puts $::circle
}
#將畫布上組件list清除
proc delete_circle {obj list_circle} {
foreach dc $list_circle {
$obj delete $dc
}
}

read more...