Android 学习(三)下: UI 控件
// 获得当前的时间,获得小时和分钟 Calendar calendar = Calendar.getInstance(); hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); minute = calendar.get(Calendar.MINUTE);// 获得当前的秒 year = calendar.get(Calendar.YEAR); monthOfYear = calendar.get(Calendar.MONTH); dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
public void onClick(View v) { switch (v.getId()) { case R.id.button1: TimePickerDialog timePickerDialog = new TimePickerDialog(Main.this, new MyTimePickerDialog(), hourOfDay, minute, true); timePickerDialog.show();// 显示对话框 break; case R.id.button2: DatePickerDialog datePickerDialog = new DatePickerDialog(Main.this, new MyDatePickerDialog(), year, monthOfYear, dayOfMonth); datePickerDialog.show();// 显示对话框 break; } } public class MyDatePickerDialog implements DatePickerDialog.OnDateSetListener { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(Main.this, "year:" + year + "monthOfYear:" + monthOfYear + "dayOfMonth:" + dayOfMonth, 1).show(); } } public class MyTimePickerDialog implements TimePickerDialog.OnTimeSetListener { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(Main.this, "hourOfDay:" + hourOfDay + "minute:" + minute, 1).show(); } }
// 如何设置窗口有刻度的效果 requestWindowFeature(Window.FEATURE_PROGRESS); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.main); progressBar = (ProgressBar) this.findViewById(R.id.progressbar); setProgressBarVisibility(true); setProgressBarIndeterminate(true); setProgress(3500);
public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: progressBar.setProgress((int) (progressBar.getProgress() * 1.2)); progressBar.setSecondaryProgress((int) (progressBar .getSecondaryProgress() * 1.2)); break; case R.id.button2: progressBar.setProgress((int) (progressBar.getProgress() * 0.8)); progressBar.setSecondaryProgress((int) (progressBar .getSecondaryProgress() * 0.8)); break; } }
ratingBar.setMax(100);// 设置最大刻度 ratingBar.setProgress(20);// 设置当前的刻度 ratingBar.setOnRatingBarChangeListener(this);
@Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { // TODO Auto-generated method stub int progress = ratingBar.getProgress(); Toast.makeText(Main.this, "progress:" + progress + "rating:" + rating, 1).show(); }
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="fill_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="滚动视图" android:textSize="30dp"></TextView> </LinearLayout> </ScrollView>
<?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/item1"></ImageView> </LinearLayout> </HorizontalScrollView>
package com.android.mygallery; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class Main extends Activity { /** Called when the activity is first created. */ private Gallery gallery; private ImageAdapter imageAdapter; // 声明图片的数组 private int[] resIds = { R.drawable.item1, R.drawable.item2, R.drawable.item3, R.drawable.item4, R.drawable.item5, R.drawable.item6, R.drawable.item7, R.drawable.item8, R.drawable.item9, R.drawable.item10, R.drawable.item11, R.drawable.item12, R.drawable.item13, R.drawable.item14, R.drawable.item15 }; // android的适配器 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gallery = (Gallery) this.findViewById(R.id.gallery); imageAdapter = new ImageAdapter(this); gallery.setAdapter(imageAdapter); } public class ImageAdapter extends BaseAdapter { private Context context; int mGralleyItemBackground;// 使用简单的计数器,填充背景图片 public ImageAdapter(Context context) { this.context = context; // 读取属性 TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery); mGralleyItemBackground = typedArray.getResourceId( R.styleable.Gallery_android_galleryItemBackground, 0); } @Override public int getCount() { // TODO Auto-generated method stub return Integer.MAX_VALUE; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return resIds[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 // 自定义的适配器,需要用自定义的布局来显示,通常android的通用布局是不能满足我们的需求 // 可以手工创建一个View视图,也可以inflate填充一个XML // 从数据源中根据position 获得每一个Item的值,填充到指定的XML布局中 // View convertView 是一个旧的布局,如果没有新的布局填充的时候,将使用旧的布局 // 当前的布局,会被追加到父布局中 ImageView imageView = new ImageView(context); imageView.setImageResource(resIds[position % resIds.length]); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setLayoutParams(new Gallery.LayoutParams(136, 88)); imageView.setBackgroundResource(mGralleyItemBackground); return imageView; } } }
package com.android.adapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.android.myspinner.R; public class MyAdapter { public MyAdapter() {} public static List<String> getData() { List<String> list = new ArrayList<String>(); list.add("北京"); list.add("上海"); list.add("广州"); return list; } public static List<Map<String, Object>> getListMaps() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("ivLogo", R.drawable.calendar); map1.put("applicationName", "日历"); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("ivLogo", R.drawable.eoemarket); map2.put("applicationName", "eoemarket客户端"); list.add(map1); list.add(map2); return list; } }
spinner = (Spinner) this.findViewById(R.id.spinner); List<String> list = MyAdapter.getData(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(Main.this, android.R.layout.simple_spinner_item, list); spinner.setAdapter(adapter); spinner2 = (Spinner) this.findViewById(R.id.spinner2); // List<Map<String,Object>> List<Map<String, Object>> listmaps = MyAdapter.getListMaps(); SimpleAdapter simpleAdapter = new SimpleAdapter(Main.this, listmaps, R.layout.item, new String[] { "ivLogo", "applicationName" }, new int[] { R.id.imageview, R.id.textview }); spinner2.setAdapter(simpleAdapter); spinner2.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub String appName = ((Map<String, Object>) spinner2 .getItemAtPosition(position)).get("applicationName") .toString(); setTitle(appName); } @Override public void onNothingSelected(AdapterView<?> parent) { } });
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/pname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_weight="1" android:textSize="15sp" android:text="产品名称" /> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_weight="1" android:textSize="15sp" android:text="产品价格" /> <TextView android:id="@+id/address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_weight="1" android:textSize="15sp" android:text="产品产地" /> </LinearLayout> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listview" /> </LinearLayout>
package com.android.android_listview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MyDataSource { public MyDataSource() {} public static List<Map<String, String>> getMaps() { List<Map<String, String>> listMaps = new ArrayList<Map<String,String>>(); Map<String, String> map1 = new HashMap<String, String>(); map1.put("pname", "西瓜"); map1.put("price", "$2.30"); map1.put("address", "广西"); Map<String, String> map2 = new HashMap<String, String>(); map2.put("pname", "香蕉"); map2.put("price", "$9.30"); map2.put("address", "浙江"); Map<String, String> map3 = new HashMap<String, String>(); map3.put("pname", "苹果"); map3.put("price", "$99.99"); map3.put("address", "USA"); listMaps.add(map1); listMaps.add(map2); listMaps.add(map3); return listMaps; } }
package com.android.android_listview; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private ListView listView; private SimpleAdapter adapter; private List<Map<String, String>> data = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) this.findViewById(R.id.listview); data = MyDataSource.getMaps(); adapter = new SimpleAdapter(MainActivity.this, data, R.layout.activity_main, new String[] { "pname", "price", "address" }, new int[] { R.id.pname, R.id.price, R.id.address }); listView.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。