編輯:關於Android編程
// 獲得當前的時間,獲得小時和分鐘 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(); } }·ProgressBar: OnCreate下:
// 如何設置窗口有刻度的效果 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 評分控件 OnCreate下
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(); }·ScrollView 垂直滾動控件
·HorizontalScrollView 水平滾動控件
·Gallery 畫廊控件 ·只能顯示一行,而且支持水平滑動效果
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; } } }·PopupWindow ·?PopupWindow 可以創建類似對話框風格的窗口,使用PopupWindow創建對話框風格的窗口只要實現兩個步驟即可: ·?1、調用PopupWindow的構造器創建PopupWindow對象 ·?2、調用PopupWindow的showAsDropDown(View view)方法將PopupWindow作為view的組件的下拉組件顯示出來,或者調用PopupWindow的showAtLocation方法將PopupWindow在指定的位置顯示出來。 · ·ImageSwitcher圖片切換控件 ·?ImageSwitcher控件可以用在不同的圖像之間切換,其中切換的過程可以采用動畫的方法,如淡入淡出的效果。 ·?ImageSwitcher需要一個圖像工廠(ViewFactory)來創建用於顯示圖像的ImageView對象,因此我們需要一個實現android.widget.ViewSwitcher.ViewFactory接口的類。 · ·GridView網格控件 ·?GridView控件用於顯示一個網格圖像, GridView主要是用在一些相冊的布局顯示圖片。 ·?GridView采用的是二維表的方式顯示單元格,就需要設置二維表的行和列。設置GridView的列可以使用
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·2.OnCreate當中實現:getData() { List list = new ArrayList (); list.add("北京"); list.add("上海"); list.add("廣州"); return list; } public static List
spinner = (Spinner) this.findViewById(R.id.spinner); List·TabHost標簽控件 ?如果在屏幕上要放置很多的控件,可能一個屏放不下,除了使用滾動視圖的方式外,還可以使用標簽控件對屏幕進行分頁顯示,當單擊標簽控件的不同標簽時,會顯示當前標簽的內容, 在android系統中一個標簽可以是一個View或者是Activity ?TabHost是標簽控件的核心類,也是一個標簽的集合,每一個標簽是TabHost.TabSpec對象。通過TabHost類的addTab的方法添加多個TabHost.TabSpec對象。 · ·ViewStub惰性裝載控件 ?之前我們介紹過list = MyAdapter.getData(); ArrayAdapter adapter = new ArrayAdapter (Main.this, android.R.layout.simple_spinner_item, list); spinner.setAdapter(adapter); spinner2 = (Spinner) this.findViewById(R.id.spinner2); // List > List > 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 ) spinner2 .getItemAtPosition(position)).get("applicationName") .toString(); setTitle(appName); } @Override public void onNothingSelected(AdapterView> parent) { } });
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·MainActivity.java> getMaps() { List > listMaps = new ArrayList >(); Map map1 = new HashMap (); map1.put("pname", "西瓜"); map1.put("price", "$2.30"); map1.put("address", "廣西"); Map map2 = new HashMap (); map2.put("pname", "香蕉"); map2.put("price", "$9.30"); map2.put("address", "浙江"); Map map3 = new HashMap (); 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· · · Over> 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; } }
Android OkHttp(一)初識,這篇文章最後提供了一個封裝Okhttp請求的類,今天就來看看在項目中具體的使用情況。一、簡單接口請求。接口請求,需要有一個服務端,
當我們看到一款UI布局很漂亮的APP,想要了解別人是怎麼實現的時候,就可以通過Apktool工具來反編譯別人的apk,從而獲取圖片及布局資源來進行學習。 其實我們下載
在Android開發中,Animation是用來給控件制作效果的。大多數的控件都可以用這個類,這個類包含了4種基本動作,分別為移動,旋轉,淡入淡出,縮放。在使用Anima
1、傳感器簡介手機中內置的傳感器是一種微型的物理設備,它能夠探測、感受到外界的信號,並按一定規律轉換成我們所需要的信息。Android手機通常都會支持多種類型的傳感器,如