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对象,扫描的方式

 
 package net.blogjava.mobile;

 import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView; public class Main extends Activity implements OnClickListener
{
private String[] provinces = new String[]
{ "辽宁省", "山东省", "河北省", "福建省", "广东省", "黑龙江省" };
private ButtonOnClick buttonOnClick = new ButtonOnClick(1);
private ListView lv = null;
private void showListDialog()
{
new AlertDialog.Builder(this).setTitle("选择省份").setItems(provinces,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
final AlertDialog ad = new AlertDialog.Builder(
Main.this).setMessage(
"您已经选择了: " + which + ":" + provinces[which])
.show();
android.os.Handler hander = new android.os.Handler();
hander.postDelayed(new Runnable()
{
@Override
public void run()
{
ad.dismiss(); }
}, 5 * 1000); }
}).show();
} private void showSingleChoiceDialog()
{ new AlertDialog.Builder(this).setTitle("选择省份").setSingleChoiceItems(
provinces, 1, buttonOnClick).setPositiveButton("确定",
buttonOnClick).setNegativeButton("取消", buttonOnClick).show(); } private void showMultiChoiceDialog()
{ AlertDialog ad = new AlertDialog.Builder(this)
.setIcon(R.drawable.image).setTitle("选择省份")
.setMultiChoiceItems(provinces, new boolean[]
{ false, true, false, true, false, false },
new DialogInterface.OnMultiChoiceClickListener()
{
public void onClick(DialogInterface dialog,
int whichButton, boolean isChecked)
{ }
}).setPositiveButton("确定",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,
int whichButton)
{
int count = lv.getCount();
String s = "您选择了:";
for (int i = 0; i < provinces.length; i++)
{ if (lv.getCheckedItemPositions().get(i))
s += i + ":"
+ lv.getAdapter().getItem(i)
+ " "; }
if (lv.getCheckedItemPositions().size() > 0)
{
new AlertDialog.Builder(Main.this)
.setMessage(s).show();
}
else
{
new AlertDialog.Builder(Main.this)
.setMessage("您未选择任何省份").show(); } }
}).setNegativeButton("取消", null).create();
lv = ad.getListView();
ad.show(); } private class ButtonOnClick implements DialogInterface.OnClickListener
{
private int index; public ButtonOnClick(int index)
{
this.index = index;
} @Override
public void onClick(DialogInterface dialog, int whichButton)
{
if (whichButton >= 0)
{
index = whichButton;
// dialog.cancel();
}
else
{
if (whichButton == DialogInterface.BUTTON_POSITIVE)
{
new AlertDialog.Builder(Main.this).setMessage(
"您已经选择了: " + index + ":" + provinces[index]).show();
}
else if (whichButton == DialogInterface.BUTTON_NEGATIVE)
{
new AlertDialog.Builder(Main.this).setMessage("您什么都未选择.")
.show(); }
} } } @Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.btnListDialog:
{
showListDialog();
break;
}
case R.id.btnSingleChoiceDialog:
{
showSingleChoiceDialog();
break;
}
case R.id.btnMultiChoiceDialog:
{
showMultiChoiceDialog();
break;
}
}
} @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnListDialog = (Button) findViewById(R.id.btnListDialog);
Button btnSingleChoiceDialog = (Button) findViewById(R.id.btnSingleChoiceDialog);
Button btnMultiChoiceDialog = (Button) findViewById(R.id.btnMultiChoiceDialog);
btnListDialog.setOnClickListener(this);
btnSingleChoiceDialog.setOnClickListener(this);
btnMultiChoiceDialog.setOnClickListener(this);
}
}
上一篇:利用iframe无刷新上传文件的坑


下一篇:Cmake用法