tab:
--------------------------------
public class TabTestActivity extends TabActivity implements TabHost.TabContentFactory{
private ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost th=this.getTabHost();
th.addTab(th.newTabSpec("中国人").setIndicator("中国歌曲").setContent(this));
th.addTab(th.newTabSpec("外国人").setIndicator("外国歌曲").setContent(this));
th.addTab(th.newTabSpec("周杰伦").setIndicator("流行歌曲").setContent(this));
}
public View createTabContent(String tag) {
lv=new ListView(this);
List<String> l=new ArrayList<String>();
l.add(tag);
if(tag.equals("中国人")){
l.add("黄湖水");
l.add("国歌");
}else if(tag.equals("外国人")){
l.add("heyho");
l.add("my love");
}else if(tag.equals("周杰伦")){
l.add("断了的弦");
l.add("黑色幽默");
}
ArrayAdapter a=new ArrayAdapter(this, android.R.layout.simple_list_item_checked,l);
lv.setAdapter(a);
return lv;
}
}
--------------------------------------------------------------------------------
seekbar:
package com.yinhe;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class SeekActivity extends Activity{
private SeekBar seekbar;
private TextView tv1,tv2;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.seek);
seekbar=(SeekBar)this.findViewById(R.id.seekbar);
tv1=(TextView)this.findViewById(R.id.seek_tv1);
tv2=(TextView)this.findViewById(R.id.seek_tv2);
seekbar.setOnSeekBarChangeListener(l);
}
private OnSeekBarChangeListener l=new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
tv2.setText("停止调试");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
tv2.setText("开始调试……");
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
tv1.setText("当前进度:"+progress);
}
};
}
------------------------------------------------------------------------
progress:
package com.yinhe;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class ProgressActivity extends Activity {
private ProgressBar pb;
private Button b1,b2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showView();
setListener();
}
public void showView(){
b1=(Button)this.findViewById(R.id.Button01);
b2=(Button)this.findViewById(R.id.Button02);
pb=(ProgressBar)this.findViewById(R.id.ProgressBar01);
}
public void setListener(){
b1.setOnClickListener(l1);
b2.setOnClickListener(l2);
}
private OnClickListener l1=new OnClickListener() {
public void onClick(View v) {
pb.incrementProgressBy(1);
}
};
private OnClickListener l2=new OnClickListener() {
public void onClick(View v) {
pb.incrementProgressBy(-1);
}
};
}
--------------------------------------------------------------------
grid:
package com.yinhe;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class GridActivity extends Activity {
//private GridView gv;
private Gallery gv;
private ImageView iv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.grid);
showView();
}
public void showView(){
gv=(Gallery)this.findViewById(R.id.grid_gv);
//gv.setNumColumns(3);
gv.setAdapter(new MyAdapter(this));
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(GridActivity.this, "您选择了第"+(arg2+1)+"号图片", Toast.LENGTH_SHORT).show();
}
});
}
class MyAdapter extends BaseAdapter{
Integer[] imgs={
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5,
R.drawable.img6,
R.drawable.img7,
R.drawable.img8,
R.drawable.img1
};
Context context;
public MyAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return imgs.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
iv=new ImageView(context);
//iv.setLayoutParams(new LayoutParams(100, 100));
iv.setPadding(1, 20, 1, 20);
}else{
iv=(ImageView)convertView;
}
iv.setImageResource(imgs[position]);
return iv;
}
}
}
-----------------------------------------------------------------------------
data_time:
package com.yinhe;
import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class DateActivity extends Activity {
private TextView tv1,tv2;
private Button b1,b2;
private Calendar c;
private int year,month,day,hour,minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.date);
showView();
setListener();
}
public void showView(){
b1=(Button)this.findViewById(R.id.b1);
b2=(Button)this.findViewById(R.id.b2);
c=Calendar.getInstance(Locale.CHINA);
year=c.get(Calendar.YEAR);
month=c.get(Calendar.MONTH);
day=c.get(Calendar.DAY_OF_MONTH);
hour=c.get(Calendar.HOUR);
minute=c.get(Calendar.MINUTE);
tv1=(TextView)this.findViewById(R.id.tv1);
tv1.setText(year+":"+(month+1)+":"+day);
tv2=(TextView)this.findViewById(R.id.tv2);
tv2.setText(hour+":"+minute);
}
public void setListener(){
b1.setOnClickListener(l1);
b2.setOnClickListener(l2);
}
private OnClickListener l1=new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(0);
}
};
private OnClickListener l2=new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(1);
}
};
@Override
protected Dialog onCreateDialog(int id) {
if(id==0){
return new DatePickerDialog(this, bl1, year, month, day);
}else{
return new TimePickerDialog(this, bl2, hour, minute, false);
}
}
private OnDateSetListener bl1=new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
year=year;
month=monthOfYear+1;
day=dayOfMonth;
tv1.setText(year+":"+month+":"+day);
}
};
private OnTimeSetListener bl2=new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hour=hourOfDay;
minute=minute;
tv2.setText(hour+":"+minute);
}
};
}
---------------------------------------------------------------------------
preferences:
package com.yinhe;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
public class MusicActivity extends Activity {
private TextView tv;
private boolean flag=false;
private DIMPlayer d=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.music);
tv=(TextView)this.findViewById(R.id.music_tv);
d=new DIMPlayer(this);
SharedPreferences sp=this.getSharedPreferences("music",MODE_PRIVATE);
flag=sp.getBoolean("flag", false);
if(flag){
tv.setText("当前播放状态:开");
d.startPlay();
}else{
tv.setText("当前播放状态:关");
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==event.KEYCODE_HOME){
SharedPreferences sp=this.getSharedPreferences("music",MODE_PRIVATE);
Editor editor=sp.edit();
editor.putBoolean("flag", flag);
editor.commit();
d.pausePlay();
d.finishPlay();
this.finish();
}
return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch(keyCode){
case 19:
if(flag){
Toast.makeText(this, "已经是播放状态", Toast.LENGTH_SHORT).show();
}else{
flag=true;
tv.setText("当前播放状态:开");
d.startPlay();
}
break;
case 20:
if(!flag){
Toast.makeText(this, "已经是关闭状态", Toast.LENGTH_SHORT).show();
}else{
flag=false;
tv.setText("当前播放状态:关");
d.pausePlay();
}
break;
}
return true;
}
}
package com.yinhe;
import java.io.IOException;
import android.content.Context;
import android.media.MediaPlayer;
public class DIMPlayer {
private MediaPlayer mPlayer;
private Context context;
public DIMPlayer(Context context){
this.context=context;
mPlayer=MediaPlayer.create(context,R.raw.a);
}
public void startPlay(){
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPlayer.start();
}
public void pausePlay(){
mPlayer.pause();
}
public void finishPlay(){
mPlayer.stop();
mPlayer.release();
}
}
--------------------------------------------------------------------
file:
package com.yinhe;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
public class FileMusicActivity extends Activity {
private TextView tv;
private boolean flag=false;
private DIMPlayer d=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.music);
tv=(TextView)this.findViewById(R.id.music_tv);
d=new DIMPlayer(this);
load();
if(flag){
tv.setText("当前播放状态:开");
d.startPlay();
}else{
tv.setText("当前播放状态:关");
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==event.KEYCODE_BACK){
save();
d.pausePlay();
d.finishPlay();
this.finish();
}
return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch(keyCode){
case 19:
if(flag){
Toast.makeText(this, "已经是播放状态", Toast.LENGTH_SHORT).show();
}else{
flag=true;
tv.setText("当前播放状态:开");
d.startPlay();
}
break;
case 20:
if(!flag){
Toast.makeText(this, "已经是关闭状态", Toast.LENGTH_SHORT).show();
}else{
flag=false;
tv.setText("当前播放状态:关");
d.pausePlay();
}
break;
}
return true;
}
public void load(){
Properties pros=new Properties();
try {
FileInputStream fis=this.openFileInput("music.properties");
pros.load(fis);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(pros.get("music")!=null){
flag=Boolean.valueOf(pros.get("music").toString());
}
}
public void save(){
Properties pros=new Properties();
pros.put("music", String.valueOf(flag));
try {
FileOutputStream fos=this.openFileOutput("music.properties", Context.MODE_WORLD_WRITEABLE);
pros.store(fos, "");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.yinhe;
import java.io.IOException;
import android.content.Context;
import android.media.MediaPlayer;
public class DIMPlayer {
private MediaPlayer mPlayer;
private Context context;
public DIMPlayer(Context context){
this.context=context;
mPlayer=MediaPlayer.create(context,R.raw.a);
}
public void startPlay(){
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPlayer.start();
}
public void pausePlay(){
mPlayer.pause();
}
public void finishPlay(){
mPlayer.stop();
mPlayer.release();
}
}