Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3436877
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: Java

2011-11-30 17:40:37

31.动态更改屏幕方向
    /如果是竖排,则改为横排  
    if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)  
    {  
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
    }  
 
    //如果是横排,则改为竖排  
 
    else if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)  
    {  
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
    }  
        
        在AndroidManifest.xml文件里设置默认方向
                                        android:label="@string/app_name"  
                  android:screenOrientation="portrait">

32.防止旋屏后重新执行onCreate
   只需要在Activity 的配置文件里添加属性:
        android:configChanges="orientation|keyboardHidden|navigation"
        
        可以在activity中重载onConfigurationChanged方法,根据不同旋转方向做其他动作,如下:

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            /*
            if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            }
            else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            }*/
        }

33.如何让ListView的滚动条定位到最后一行    
        lv.setSelection(list.size() - 1);

34.视频录制暂停,续存
    录制程序,我是放到了onCreate中:
    fout = new FileOutputStream(myRecAudioFile,true);第二个参数bool  append为打开方式
    在录制程序中,开始录制时,设置输出文件
    recorder.setOutputFile(fout.getFD());
    
35.从xml中取字符串
    getResources().getString(R.string.sendmsg)

36.ListView用BaseAdapter绑定后无法选中某行
   如果view项中有按钮,又想检测listview的点击,需要把按钮放到布局中,并设置按钮属性Focusable( in touch mode)为false,
   设置布局属性Descendant quality为blocksDescendants

37.字符串转long型
    Long.parseLong(string)
    
38.获取当前时间
          SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
          sDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/beijing"));
          String date = sDateFormat.format(new java.util.Date());   

39.提取缩略图
       
       Bitmap createVideoThumbnail(String filePath, int kind)
      从方法名称即可看出,这个方法用于生成视频缩略图。
        参数:
          filePath: 视频文件路径
          kind:  文件种类,可以是 MINI_KIND 或 MICRO_KIND

    Bitmap extractThumbnail(Bitmap source, int width, int height, int options)
      此方法用于生成一个指定大小的图片缩略图。
        参数:
          source: 需要被创造缩略图的源位图对象
          width: 生成目标的宽度
          height: 生成目标的高度
          options:在缩略图抽取时提供的选项

    Bitmap extractThumbnail(Bitmap source, int width, int height)
      此方法用于生成一个指定大小的图片缩略图。
        参数:
          source: 需要被创造缩略图的源位图对象
          width: 生成目标的宽度
          height: 生成目标的高度

        例如:Bitmap bp=ThumbnailUtils.createVideoThumbnail(filePath, Thumbnails.MICRO_KIND);

40.提示框
            Toast.makeText(this, "这是一个提示", Toast.LENGTH_SHORT).show();
            //从资源文件string.xml 里面取提示信息
            Toast.makeText(this, getString(R.string.welcome), Toast.LENGTH_SHORT).show();

            带一个确定的对话框

            new AlertDialog.Builder(this)
          .setMessage("这是第二个提示")
          .setPositiveButton("确定",
                         new DialogInterface.OnClickListener(){
                                 public void onClick(DialogInterface dialoginterface, int i){
                                     //按钮事件
                                  }
                          })
          .show();
                AlertDialog.Builder 还有很多复杂的用法,有确定和取消的对话框

                new AlertDialog.Builder(this)
         .setTitle("提示")
         .setMessage("确定退出?")
         .setIcon(R.drawable.quit)
         .setPositiveButton("确定", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
         setResult(RESULT_OK);//确定按钮事件
         finish();
         }
         })
         .setNegativeButton("取消", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
         //取消按钮事件
         }
         })
         .show();

41.从文件路径中提取文件名和文件夹名
    String path = "\sdcard\xxx\a.txt";  
    
    //取文件夹名字
    String directoryStr = path.substring(0, path.lastIndexOf(File.separator));  
    //获取文件名  
         String directoryStr2 = path.substring(path.lastIndexOf(File.separator)+1);  

42.循环遍历map的方法   
   // 遍历方法一 hashmap entrySet() 遍历   

  System.out.println("方法一");  

  Iterator it = tempMap.entrySet().iterator();  

  while (it.hasNext()) {  

   Map.Entry entry = (Map.Entry) it.next();  

   Object key = entry.getKey();  

   Object value = entry.getValue();  

   System.out.println("key=" + key + " value=" + value);  

  }  

  System.out.println("");


 

  // JDK1.5中,应用新特性For-Each循环   

  // 遍历方法二   

  System.out.println("方法二");  

  for (Map.Entry entry : tempMap.entrySet()) {  

   String key = entry.getKey().toString();  

   String value = entry.getValue().toString();  

   System.out.println("key=" + key + " value=" + value);  

  }  

  System.out.println("");  

 

  // 遍历方法三 hashmap keySet() 遍历   

  System.out.println("方法三");  

  for (Iterator i = tempMap.keySet().iterator(); i.hasNext();) {  

   Object obj = i.next();  

   System.out.println(obj);// 循环输出key   

   System.out.println("key=" + obj + " value=" + tempMap.get(obj));  

  }  

  for (Iterator i = tempMap.values().iterator(); i.hasNext();) {  

   Object obj = i.next();  

   System.out.println(obj);// 循环输出value   

  }  

  System.out.println("");  

 

  // 遍历方法四 treemap keySet()遍历   

  System.out.println("方法四");  

  for (Object o : tempMap.keySet()) {  

   System.out.println("key=" + o + " value=" + tempMap.get(o));  

  }  

43.Android读写文件
   一、 从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写)
        String res = "";

        try{
            InputStream in = getResources().openRawResource(R.raw.bbi);
            //InputStream in = getResources().getAssets().open(fileName);
            //在\Test\res\raw\bbi.txt,
       int length = in.available();    
       byte [] buffer = new byte[length];    
       in.read(buffer);    
       res = EncodingUtils.getString(buffer, "UTF-8");        
       in.close();
   }catch(Exception e){
      e.printStackTrace();
   }

        二、写, 读sdcard目录上的文件,要用FileOutputStream,不能用openFileOutput
        //写在/mnt/sdcard/目录下面的文件
           public void writeFileSdcard(String fileName,String message){
           try{
         //FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
         FileOutputStream fout = new FileOutputStream(fileName);
         byte [] bytes = message.getBytes();
         fout.write(bytes);
         fout.close();
        }
        catch(Exception e){
                    e.printStackTrace();
       }
   }
   //读在/mnt/sdcard/目录下面的文件
   public String readFileSdcard(String fileName){
        String res="";
        try{
             FileInputStream fin = new FileInputStream(fileName);
             int length = fin.available();
             byte [] buffer = new byte[length];
             fin.read(buffer);
             res = EncodingUtils.getString(buffer, "UTF-8");
             fin.close();
        }
        catch(Exception e){
               e.printStackTrace();
        }
        return res;
   }
   
    三、从sdcard中去读文件
        String fileName = "/sdcard/Y.txt";
        String res="";
        try{
                FileInputStream fin = new FileInputStream(fileName);
            int length = fin.available();
            byte [] buffer = new byte[length];
                fin.read(buffer);
                res = EncodingUtils.getString(buffer, "UTF-8");
                fin.close();
        }catch(Exception e){
               e.printStackTrace();
        }
 
    四、写文件, 一般写在/data/data/里面
   public voidwriteFileData(String fileName,String message){
       try{
            FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
            byte [] bytes = message.getBytes();
            fout.write(bytes);
             fout.close();
        }
       catch(Exception e){
            e.printStackTrace();
       }
   }   
阅读(1457) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~