Android零碎知识(一)
public abstract Resources getResources ()
Return a Resources instance for your application's package.
BitmapFactory extends Object java.lang.Object ↳ android.graphics.BitmapFactory Class Overview -------------------------------------------------------------------------------- Creates Bitmap objects from various sources, including files, streams, and byte-arrays.
public void translate (float dx, float dy) Added in API level 1 Preconcat the current matrix with the specified translation When working with matrices, the word concatenation refers to multiplication. Since matrix multiplication is not commutative, there is a separate word for backwards multiplication. Pre-concatenating a to b means setting a = b × a. (As opposed to a = a × b, which will give a different matrix) The canvas maintains a matrix and the canvas is manipulated by methods which are well defined in OpenGL
三种列表对话框:setItems,setItems,setMultiChoiceItems。
在 接口DialogInterface.OnClickListener的抽象方法
public abstract void onClick (DialogInterface dialog, int which) 中的参数which:The button that was clicked (e.g. BUTTON1
)
or the position of the item clicked
既能监听列表项(>=0),又能监听按钮(<=0)
BUTTON_NEGATIVE=-2,BUTTON_NEUTRAL=-3,BUTTON_POSITIVE=-1;
public AlertDialog.Builder setSingleChoiceItems (CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener)
其中参数checkedItem specifies which item is checked. If -1 no items are checked.因为有默认选项,onclick方法若不点击列表项,这个默认值就获取不到,所以在onclick方法里需要设置index的默认值为1。下面的程序是在构造方法传入1的。
另外注意在多选列表对话框中是如何获得用户选择的列表项,先得到对话框的Listview对象,扫描的方式
1 package net.blogjava.mobile; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.content.DialogInterface; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.ListView; 11 12 public class Main extends Activity implements OnClickListener 13 { 14 private String[] provinces = new String[] 15 { "辽宁省", "山东省", "河北省", "福建省", "广东省", "黑龙江省" }; 16 private ButtonOnClick buttonOnClick = new ButtonOnClick(1); 17 private ListView lv = null; 18 private void showListDialog() 19 { 20 new AlertDialog.Builder(this).setTitle("选择省份").setItems(provinces, 21 new DialogInterface.OnClickListener() 22 { 23 public void onClick(DialogInterface dialog, int which) 24 { 25 final AlertDialog ad = new AlertDialog.Builder( 26 Main.this).setMessage( 27 "您已经选择了: " + which + ":" + provinces[which]) 28 .show(); 29 android.os.Handler hander = new android.os.Handler(); 30 hander.postDelayed(new Runnable() 31 { 32 @Override 33 public void run() 34 { 35 ad.dismiss(); 36 37 } 38 }, 5 * 1000); 39 40 } 41 }).show(); 42 } 43 44 private void showSingleChoiceDialog() 45 { 46 47 new AlertDialog.Builder(this).setTitle("选择省份").setSingleChoiceItems( 48 provinces, 1, buttonOnClick).setPositiveButton("确定", 49 buttonOnClick).setNegativeButton("取消", buttonOnClick).show(); 50 51 } 52 53 54 private void showMultiChoiceDialog() 55 { 56 57 AlertDialog ad = new AlertDialog.Builder(this) 58 .setIcon(R.drawable.image).setTitle("选择省份") 59 .setMultiChoiceItems(provinces, new boolean[] 60 { false, true, false, true, false, false }, 61 new DialogInterface.OnMultiChoiceClickListener() 62 { 63 public void onClick(DialogInterface dialog, 64 int whichButton, boolean isChecked) 65 { 66 67 } 68 }).setPositiveButton("确定", 69 new DialogInterface.OnClickListener() 70 { 71 public void onClick(DialogInterface dialog, 72 int whichButton) 73 { 74 int count = lv.getCount(); 75 String s = "您选择了:"; 76 for (int i = 0; i < provinces.length; i++) 77 { 78 79 if (lv.getCheckedItemPositions().get(i)) 80 s += i + ":" 81 + lv.getAdapter().getItem(i) 82 + " "; 83 84 } 85 if (lv.getCheckedItemPositions().size() > 0) 86 { 87 new AlertDialog.Builder(Main.this) 88 .setMessage(s).show(); 89 } 90 else 91 { 92 new AlertDialog.Builder(Main.this) 93 .setMessage("您未选择任何省份").show(); 94 95 } 96 97 } 98 }).setNegativeButton("取消", null).create(); 99 lv = ad.getListView(); 100 ad.show(); 101 102 } 103 104 private class ButtonOnClick implements DialogInterface.OnClickListener 105 { 106 private int index; 107 108 public ButtonOnClick(int index) 109 { 110 this.index = index; 111 } 112 113 @Override 114 public void onClick(DialogInterface dialog, int whichButton) 115 { 116 if (whichButton >= 0) 117 { 118 index = whichButton; 119 // dialog.cancel(); 120 } 121 else 122 { 123 if (whichButton == DialogInterface.BUTTON_POSITIVE) 124 { 125 new AlertDialog.Builder(Main.this).setMessage( 126 "您已经选择了: " + index + ":" + provinces[index]).show(); 127 } 128 else if (whichButton == DialogInterface.BUTTON_NEGATIVE) 129 { 130 new AlertDialog.Builder(Main.this).setMessage("您什么都未选择.") 131 .show(); 132 133 } 134 } 135 136 } 137 138 } 139 140 @Override 141 public void onClick(View view) 142 { 143 switch (view.getId()) 144 { 145 case R.id.btnListDialog: 146 { 147 showListDialog(); 148 break; 149 } 150 case R.id.btnSingleChoiceDialog: 151 { 152 showSingleChoiceDialog(); 153 break; 154 } 155 case R.id.btnMultiChoiceDialog: 156 { 157 showMultiChoiceDialog(); 158 break; 159 } 160 } 161 } 162 163 @Override 164 public void onCreate(Bundle savedInstanceState) 165 { 166 super.onCreate(savedInstanceState); 167 setContentView(R.layout.main); 168 Button btnListDialog = (Button) findViewById(R.id.btnListDialog); 169 Button btnSingleChoiceDialog = (Button) findViewById(R.id.btnSingleChoiceDialog); 170 Button btnMultiChoiceDialog = (Button) findViewById(R.id.btnMultiChoiceDialog); 171 btnListDialog.setOnClickListener(this); 172 btnSingleChoiceDialog.setOnClickListener(this); 173 btnMultiChoiceDialog.setOnClickListener(this); 174 } 175 }