Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1078062
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-03-03 11:37:37

  1. package com.cloay.pt.ui;  
  2.   
  3. import java.io.FileNotFoundException;  
  4. import java.io.IOException;  
  5.   
  6. import android.content.ContentResolver;  
  7. import android.content.Intent;  
  8. import android.content.SharedPreferences;  
  9. import android.graphics.Bitmap;  
  10. import android.graphics.BitmapFactory;  
  11. import android.net.Uri;  
  12. import android.os.Bundle;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.ImageView;  
  16. import android.widget.RatingBar;  
  17. import android.widget.RatingBar.OnRatingBarChangeListener;  
  18. import android.widget.TextView;  
  19.   
  20. import com.cloay.pt.BasicActivity;  
  21. import com.cloay.pt.R;  
  22. import com.cloay.pt.constants.Constant;  
  23. import com.cloay.pt.utils.ImageUtil;  
  24.   
  25. /** 
  26.  * 游戏设置类,游戏难度等 
  27.  * @author Cloay 
  28.  * 2011-12-23 
  29.  * 下午05:21:41 
  30.  */  
  31. public class SettingsActivity extends BasicActivity{  
  32.     private SharedPreferences settings;  //保存设置  
  33.     private RatingBar ratingBar;  
  34.     private TextView defaultPicture;  
  35.     private TextView picture;  
  36.     private ImageView previewImage;  
  37.     @Override  
  38.     protected void onCreate(Bundle savedInstanceState) {  
  39.         super.onCreate(savedInstanceState);  
  40.         setContentView(R.layout.settings);  
  41.         BasicActivity.allActivity.add(this);  
  42.         //初始化数据  
  43.         initData();  
  44.     }  
  45.       
  46.     /** 
  47.      * 初始化数据 
  48.      */  
  49.     private void initData() {  
  50.         settings = getSharedPreferences("settings"0);  
  51.         ratingBar = (RatingBar) findViewById(R.id.rating);  
  52.         //游戏默认图片  
  53.         defaultPicture = (TextView) findViewById(R.id.def);  
  54.         //从图库中选择图片,自定义  
  55.         picture = (TextView) findViewById(R.id.picture);  
  56.         previewImage = (ImageView)findViewById(R.id.preview);  
  57.         if("".equals(settings.getString("picture"""))) {  
  58.             previewImage.setImageResource(R.drawable.def);  
  59.         }else{  
  60.             previewImage.setImageBitmap(ImageUtil.getBitmapFromSDCard(settings.getString("picture""")));  
  61.         }  
  62.         //设置难度选择监听  
  63.         ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {  
  64.             @Override  
  65.             public void onRatingChanged(RatingBar ratingBar, float rating,  
  66.                     boolean fromUser) {  
  67.                 settings.edit().putInt("level", (int)rating).commit();   //保存用户设置后的游戏难度  
  68.                 showToast(SettingsActivity.this, (int)rating);  
  69.             }  
  70.         });  
  71.         //设置图片选择监听  
  72.         defaultPicture.setOnClickListener(new OnClickListener() {  
  73.             @Override  
  74.             public void onClick(View v) {  
  75.                 settings.edit().putString("picture""").commit();  
  76.                 previewImage.setImageResource(R.drawable.def);  
  77.             }  
  78.         });  
  79.         picture.setOnClickListener(new OnClickListener() {  
  80.             @Override  
  81.             public void onClick(View v) {  
  82.                 Intent intent = new Intent();    
  83.                 /* 开启Pictures画面Type设定为image */    
  84.                 intent.setType("image/*");    
  85.                 /* 使用Intent.ACTION_GET_CONTENT这个Action */    
  86.                 intent.setAction(Intent.ACTION_GET_CONTENT);   
  87.                 /* 取得相片后返回本画面 */    
  88.                 startActivityForResult(intent, 1);  
  89.             }  
  90.         });  
  91.           
  92.     }  
  93.       
  94.     @Override  
  95.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  96.         if (resultCode == RESULT_OK) {    
  97.             Uri uri = data.getData();    
  98.             ContentResolver cr = this.getContentResolver();    
  99.             try {    
  100.                 Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));    
  101.                 try {  
  102.                     saveBitmap("cloay", bitmap);  
  103.                 } catch (IOException e) {  
  104.                     e.printStackTrace();  
  105.                 }  
  106.                 settings.edit().putString("picture", Constant.picPath + "cloay.png").commit();  
  107.                 previewImage.setImageBitmap(bitmap);  
  108.             } catch (FileNotFoundException e) {    
  109.                 settings.edit().putString("picture""").commit();  
  110.                 previewImage.setImageResource(R.drawable.def);  
  111.             }    
  112.         }   
  113.         super.onActivityResult(requestCode, resultCode, data);  
  114.     }  
  115. }  

通过设置setOnRatingBarChangeListener监听器来获取用户选择RatingBar的值,获取之后往往需要保存,通常使用SharedPrefence即可,也可选用SQLite等。SharedPrefence的使用详细请看: Android SharedPreferences的使用

说明:转载请注明出处!

阅读(408) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~