慕课网/安卓攻城狮视频学习及练习

卧疾丰暇豫,翰墨时间作。这篇文章主要讲述慕课网/安卓攻城狮视频学习及练习相关的知识,希望能为你提供帮助。
manifest

慕课网/安卓攻城狮视频学习及练习

文章图片

A跳转B,在B的文件中用permission权限,然后A就无权跳转到B,如果在A中用uses-permission把权限给了A,那么A就可以跳转到B了.可以通俗的说,permission给B了一把锁,A要进门,就要用uses-permission得到一把钥匙.
onitemclicklister+onscrolllistner
慕课网/安卓攻城狮视频学习及练习

文章图片

慕课网/安卓攻城狮视频学习及练习

文章图片

1 public class FActivity extends Activity implements OnItemClickListener,OnScrollListener{ 2private ListView lv; 3private SimpleAdapter sim_adapter; 4private List< Map< String, Object> > datalist; 5@Override 6 protected void onCreate(Bundle savedInstanceState) { 7// TODO Auto-generated method stub 8super.onCreate(savedInstanceState); 9setContentView(R.layout.factivity); 10 11lv=(ListView) findViewById(R.id.listView1); 12sim_adapter =new SimpleAdapter(this, getdata(), R.layout.item, new String[]{"pic","text"}, new int[]{R.id.imageView1,R.id.textView1}); 13 14lv.setAdapter(sim_adapter); 15lv.setOnItemClickListener(this); 16lv.setOnScrollListener(this); 17 18 } 19 private List< Map< String, Object> > getdata(){ 20datalist =new ArrayList< Map< String,Object> > (); 21for (int i=0; i< 10; i++) 22{ 23Map< String, Object> map = new HashMap< String, Object> (); 24map.put("pic", R.drawable.ic_launcher); 25map.put("text", "慕课网"+i); 26datalist.add(map); 27} 28return datalist; 29 } 30 @Override 31 public void onScrollStateChanged(AbsListView view, int scrollState) { 32switch (scrollState) { 33case SCROLL_STATE_FLING: 34Map< String, Object> map =new HashMap< String, Object> (); 35map.put("pic", R.drawable.ic_launcher); 36map.put("text", "增加item"); 37datalist.add(map); 38sim_adapter.notifyDataSetChanged(); 39break; 40case SCROLL_STATE_IDLE: 41 42break; 43case SCROLL_STATE_TOUCH_SCROLL: 44 45break; 46 47default: 48break; 49} 50 51 } 52 @Override 53 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 54// TODO 自动生成的方法存根 55 56 } 57 58 @Override 59 public void onItemClick(AdapterView< ?> parent, View view, int position, long id) { 60String text=lv.getItemAtPosition(position)+""; 61Toast.makeText(this, "位置:"+position+"text="+text, Toast.LENGTH_SHORT) 62.show(); 63 64 } 65 }

【慕课网/安卓攻城狮视频学习及练习】 
**Picker:选择器
DatePicker日历选择器
Timepicker时间选择器**
慕课网/安卓攻城狮视频学习及练习

文章图片

1 public class FActivity extends Activity { 2private TimePicker tp; 3private DatePicker dp; 4private Calendar cal; 5int year; 6int month; 7int day; 8int hour; 9int minute; 10 11@Override 12 protected void onCreate(Bundle savedInstanceState) { 13// TODO Auto-generated method stub 14super.onCreate(savedInstanceState); 15setContentView(R.layout.factivity); 16 17cal=Calendar.getInstance(); //生成日历类的对象实例 18year=cal.get(Calendar.YEAR); 19month=cal.get(Calendar.MONTH)+1; //注意唯一例外是month需要+1 20day=cal.get(Calendar.DAY_OF_MONTH); 21hour=cal.get(Calendar.HOUR_OF_DAY); 22minute=cal.get(Calendar.MINUTE); 23setTitle(year+"-"+month+"-"+day+" "+hour+":"+minute); 24 25tp=(TimePicker) findViewById(R.id.timePicker1); 26dp=(DatePicker) findViewById(R.id.datePicker1); 27dp.init(year, Calendar.MONTH, day, new OnDateChangedListener() { 28 29@Override 30public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 31setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth+" "+hour+":"+minute); 32 33 34} 35}); 36tp.setOnTimeChangedListener(new OnTimeChangedListener() { 37 38@Override 39public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 40 41setTitle(year+"-"+month+"-"+day+" "+hourOfDay+":"+minute); 42} 43}); 44 } 45 46 47 }

 
注意month的加减一问题。
datepicker需要用init()方法,在方法中调用监听器,而时间选择器直接调用监听器。
dialog形式
慕课网/安卓攻城狮视频学习及练习

文章图片

匿名内部类实现。
1 //参数callback是监听器 2new DatePickerDialog(this, new OnDateSetListener() { 3 4@Override 5public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 6setTitle(year+"-"+(monthOfYear+1)+"-"+dayOfMonth+" "+hour+":"+minute); 7 8} 9}, year, cal.get(Calendar.MONTH), day).show(); 10 11newTimePickerDialog(this, new OnTimeSetListener() { 12 13@Override 14public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 15setTitle(year+"-"+month+"-"+day+" "+hourOfDay+":"+minute); 16 17} 18}, hour, minute, true); 19 }

 
慕课网/安卓攻城狮视频学习及练习

文章图片

gridview
慕课网/安卓攻城狮视频学习及练习

文章图片

1 public class FActivity extends Activity implements OnItemClickListener{ 2private GridView gv; 3private SimpleAdapter sim_adapter; 4private List< Map< String, Object> > datalist; 5int[] iconid={R.drawable.calculate,R.drawable.calendar,R.drawable.clock, 6R.drawable.contact,R.drawable.message,R.drawable.money,R.drawable.notebook, 7R.drawable.rest,R.drawable.setting,R.drawable.studyhat,R.drawable.trashbox, 8R.drawable.weightscale}; 9String[] iconname={"calculate","calendar","clock","contact","message","money", 10"notebook","rest","setting","studyhat","trashbox","weightscale"}; 11@Override 12 protected void onCreate(Bundle savedInstanceState) { 13// TODO Auto-generated method stub 14super.onCreate(savedInstanceState); 15setContentView(R.layout.factivity); 16 17 18 datalist=new ArrayList< Map< String,Object> > (); 19 20gv=(GridView) findViewById(R.id.gridView1); 21sim_adapter=new SimpleAdapter(this, getdata(), R.layout.item, new String[]{"pic","text"}, new int[]{R.id.imageView1,R.id.textView1}); 22gv.setAdapter(sim_adapter); 23 24gv.setOnItemClickListener(this); 25 } 26 27 private List< Map< String, Object> > getdata() { 28 29for (int i=0; i< iconid.length; i++){ 30Map< String, Object> map=new HashMap< String, Object> (); 31map.put("pic", iconid[i]); 32map.put("text", iconname[i]); 33datalist.add(map); 34} 35 36 37return datalist; 38 39 40} 41 42 @Override 43 public void onItemClick(AdapterView< ?> parent, View view, int position, long id) { 44Toast.makeText(this, "我是"+iconname[position], Toast.LENGTH_SHORT).show(); 45 46 } 47 48 49 50 51 }

 
spinner
1 public class FActivity extends Activity implements OnItemSelectedListener{ 2private TextView txt; 3private Spinner spin; 4private List< String> list; 5private ArrayAdapter< String> arr_adapter; 6@Override 7 protected void onCreate(Bundle savedInstanceState) { 8// TODO Auto-generated method stub 9super.onCreate(savedInstanceState); 10setContentView(R.layout.factivity); 11 12txt=(TextView) findViewById(R.id.textView1); 13spin=(Spinner) findViewById(R.id.spinner1); 14list=new ArrayList< String> (); 15list.add("北京"); 16list.add("上海"); 17list.add("广州"); 18list.add("深圳"); 19arr_adapter=new ArrayAdapter< String> (this, android.R.layout.simple_spinner_item, list); 20arr_adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); 21spin.setAdapter(arr_adapter); 22spin.setOnItemSelectedListener(this); 23 24 } 25 26@Override 27public void onItemSelected(AdapterView< ?> parent, View view, int position, long id) { 28String cityname=arr_adapter.getItem(position); 29 //String name =list.get(position); 30txt.setText("我选择居住在:"+cityname); 31 32} 33@Override 34public void onNothingSelected(AdapterView< ?> parent) { 35// TODO 自动生成的方法存根 36 37} 38 39 }

 

    推荐阅读