欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

个人android笔记(三) AndroidMVCUPXML

程序员文章站 2022-07-09 18:17:52
...
1、代码片段:获得最新通话记录的时长,格式为HH:mm:ss
public String getFormattedTime() {
		long duration = 0;
		Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
				new String[] { CallLog.Calls.DURATION }, null, null,
				CallLog.Calls.DEFAULT_SORT_ORDER);

		if (cursor != null) {
			cursor.moveToPosition(0);//最新的一条
			duration = cursor.getLong(0);
		}
		String pattern = "mm:ss";
		if (duration > 3600) {
			pattern = "HH:mm:ss";
		}
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(new Date((long) duration * 1000));
	}

2、颜色的使用。
在android是可以是使用Android.Graphics.Color类的Color.BLACK、Color.RED等来设置颜色的,但这样的做法,不符合MVC将模块与资源分开的思想,故强烈建议使用在res/values/color.xml中定义color变量
<resources>
    <color name="my_red">#00ffff00</color>
</resources>

然后引用
int color = getResources().getColor(R.color.my_red);
tv.setColor(color);


3、gallery简单应用:
private Gallery gallery;
	private int[] myImage = {android.R.drawable.alert_dark_frame,
							android.R.drawable.arrow_up_float,
							android.R.drawable.btn_default_small,
							android.R.drawable.checkbox_on_background};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch3_15);
		gallery = (Gallery)findViewById(R.id.ch3_15_gallery);
		gallery.setAdapter(new ImageAdapter(this));		
	}
	
	private class ImageAdapter extends BaseAdapter{
		Context context;
		public ImageAdapter(Context context){
			this.context = context;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return myImage.length;
		}
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView iv = new ImageView(this.context);
			iv.setImageResource(myImage[position]);
			iv.setScaleType(ImageView.ScaleType.FIT_XY);
			iv.setLayoutParams(new Gallery.LayoutParams(120,120));
			return iv;
			
		}
		
	}