2.画廊
class ImageAdapter extends BaseAdapter{
private Context context; private int[] im;
public ImageAdapter(Context c,int[] im){ }
public int getCount() { }
public Object getItem(int position) { }
public long getItemId(int position) {
// TODO Auto-generated method stub // TODO Auto-generated method stub return im[position];
// TODO Auto-generated method stub return im.length; this.context = c; this.im = im;
}
return position;
public View getView(int position, View convertView, ViewGroup parent) {
} }
ImageView iv = new ImageView(context); iv.setImageResource(im[position]);
iv.setLayoutParams(new Gallery.LayoutParams(200,200)); iv.setScaleType(ImageView.ScaleType.FIT_XY); return iv;
3.谁是幸运儿
public class LuckyActivity extends Activity {
String[] names = { \许世伟\, \杜星星\, \韩坚\, \黄哲\, \何美笑\ };
int[] imgs = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.e };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.main);
final ImageView imgr = (ImageView) this.findViewById(R.id.imgr); final TextView tvr = (TextView) this.findViewById(R.id.txr); Button btr = (Button) this.findViewById(R.id.btl);
btr.setOnClickListener(new OnClickListener() { public void onClick(View v) { Random r = new Random();
int lucky = r.nextInt(names.length); tvr.setText(names[lucky]);
imgr.setImageResource(imgs[lucky]);
}
});
} }
4.计算器
public class CalculatorActivity extends Activity { String str1; String str2; int result; int result1; int result2; TextView tv; Button temp; int flag = 0;
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.tv);
int[] buttons = { R.id.bt0, R.id.bt1, R.id.bt2, R.id.bt3, R.id.bt4,
R.id.bt5, R.id.bt6, R.id.bt7, R.id.bt8, R.id.bt9 };
Button btc = (Button) this.findViewById(R.id.btc); Button btjia = (Button) this.findViewById(R.id.btjia); str1 = String.valueOf(tv.getText()); str2 = \;
Button btjian = (Button) this.findViewById(R.id.btjian); Button btchen = (Button) this.findViewById(R.id.btchen); Button btchu = (Button) this.findViewById(R.id.btchu); Button btdengyu = (Button) this.findViewById(R.id.btdengyu); btc.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) { }
String str = \; tv.setText(str);
});
for (int i = 0; i < buttons.length; i++) { }
buttonListener(btjia, 1); buttonListener(btjian, 2); buttonListener(btchen, 3); buttonListener(btchu, 4);
btdengyu.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
result2 = Integer.parseInt(str2); switch (flag) { case 1:
result = result1 + result2; break;
temp = (Button) findViewById(buttons[i]); temp.setOnClickListener(new OnClickListener() {
public void onClick(View v) { }
str2 = tv.getText().toString().trim();
str2 = str2 + String.valueOf(((Button) v).getText()); tv.setText(str2);
});
case 2:
result = result1 - result2;
break;
case 3: result = result1 * result2;
break;
case 4: result = result1 / result2; break;
}
String str = (result + \).trim(); tv.setText(str);
}
});
}
public void buttonListener(Button button, final int id) { button.setOnClickListener(new OnClickListener() { public void onClick(View v) { String str = tv.getText().toString().trim(); result1 = Integer.parseInt(str); tv.setText(\); flag = id;
}
});
}
}
5.坦克大战
public class HjgameActivity extends Activity { public void onCreate(Bundle savedInstanceState) {