1 RadioGroup控件的OnCheckedChangeListener 事件
/* RadioGroup用OnCheckedChangeListener来运行 */ mRadioGroup1.setOnCheckedChangeListener(mChangeRadio); private RadioGroup.OnCheckedChangeListener mChangeRadio = new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == mRadio1.getId()) { /* 把mRadio1的内容传到mTextView1 */ mTextView1.setText(mRadio1.getText()); myTextViewKeyListener.setTag("show soft key "); } else if (checkedId == mRadio2.getId()) { /* 把mRadio2的内容传到mTextView1 */ mTextView1.setText(mRadio2.getText()); myTextViewKeyListener.setTag(null); } } };
2 EditText.OnKeyListener 监听机制
myTextViewKeyListener.setOnKeyListener( new EditText.OnKeyListener(){ @Override public boolean onKey(View v, int keyCode, KeyEvent event) { mTextView1.setText(myTextViewKeyListener.getText().toString() ); return false; } });
3 复选框的CompoundButton.OnCheckedChangeListener监听事件
checkboxBaskball = (CheckBox) findViewById(R.id.basketball); checkboxFootball.setOnCheckedChangeListener( new MyCheckBoxListener()); private class MyCheckBoxListener implements OnCheckedChangeListener{ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ Toast.makeText(RadioGroupActivity.this, "您选择了:"+buttonView.getText(),Toast.LENGTH_SHORT).show() ; }else{ Toast.makeText(RadioGroupActivity.this, "您取消选择了:"+buttonView.getText(),Toast.LENGTH_SHORT).show() ; } } }
4 按钮的单击事件
myButton1.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { } });
4 Spinner 的 AdapterView.OnItemSelectedListener事件
private Spinner spinner; private ArrayAdapter<String> adapter; private static final String[] m={"足球","篮球","排球","乒乓球","冰球"}; public void useSpinner(){ spinner = (Spinner) findViewById(R.id.Spinner01); //将可选内容与ArrayAdapter连接起来 adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m); //设置下拉列表的风格 adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setPrompt("请选择人名"); //设置默认选择项 spinner.setVisibility(View.VISIBLE); //设置监听器 spinner.setOnItemSelectedListener(new SpinnerSelectedListener()); } //spinner 选择监听器 class SpinnerSelectedListener implements AdapterView.OnItemSelectedListener{ @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int index, long arg3) { Toast.makeText(RadioGroupActivity.this, "您选择了:"+arg0.getItemAtPosition(index).toString(),Toast.LENGTH_SHORT).show() ; } @Override public void onNothingSelected(AdapterView<?> arg0) { } }
5 时间和日期的DatePickerDialog.OnDateSetListener 和TimePickerDialog.OnTimeSetListener 监听器
final Calendar c = Calendar.getInstance(); mYear = c.get(Calendar.YEAR); mMonth = c.get(Calendar.MONTH); mDay = c.get(Calendar.DAY_OF_MONTH); mHour = c.get(Calendar.HOUR_OF_DAY); mMinute = c.get(Calendar.MINUTE); MyDatePickerDialog myDatePickerDialog = new MyDatePickerDialog(); DatePickerDialog datePickerDialog = new DatePickerDialog(this, myDatePickerDialog,mYear, mMonth, mDay); datePickerDialog.show(); MyTimePickerDialog myTimePickerDialog = new MyTimePickerDialog(); TimePickerDialog timePickerDialog = new TimePickerDialog(this, myTimePickerDialog, mHour, mMinute, true); timePickerDialog.show(); //显示时间设置对话框 /* * Function : 自定义MyDatePickerDialog类,用于实现DatePickerDialog.OnDateSetListener接口, * 当点击日期设置对话框中的“设置”按钮时触发该接口方法 */ public class MyDatePickerDialog implements DatePickerDialog.OnDateSetListener { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mYear = year; mMonth = monthOfYear; mDay = dayOfMonth; updateDateDisplay(); } } /* * Function : 自定义MyTimePickerDialog类,用于实现TimePickerDialog.OnTimeSetListener接口, * 当点击时间设置对话框中的“设置”按钮时触发该接口方法 */ public class MyTimePickerDialog implements TimePickerDialog.OnTimeSetListener { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mHour = hourOfDay; mMinute = minute; updateTimeDisplay(); } }
6 进度条使用
1) 设置进度条模式,setIndeterminate 是否为模糊模式,true:不显示进度条进度 false:显示进度条进度。
2 )设置最大进度值 setMax
3)设置现在进度值 setProgress
<ProgressBar android:id="@+id/message_detail_head_progress" android:layout_gravity="left|center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal" /> <ProgressBar android:id="@+id/message_detail_head_progress2" android:layout_gravity="left|center" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Small" />
Activity
public class ProgressBarActivity extends Activity { protected static final int GUI_STOP_NOTIFIER = 0x108; protected static final int GUI_THREADING_NOTIFIER = 0x109; private ProgressBar myProgress1; private ProgressBar myProgress2; private Button myButton; private int intCounter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.process_bar); // 设置窗口模式,因为需要显示进度条在标题栏 setProgressBarVisibility(true); // setContentView(R.layout.process_bar); // 取得ProgressBar myProgress1 = (ProgressBar) findViewById(R.id.message_detail_head_progress); myProgress2 = (ProgressBar) findViewById(R.id.message_detail_head_progress2); myButton = (Button) findViewById(R.id.Button06); myProgress1.setIndeterminate(false); myProgress2.setIndeterminate(false); myButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub myProgress1.setVisibility(View.VISIBLE); myProgress2.setVisibility(View.VISIBLE); myProgress1.setMax(100); myProgress1.setProgress(0); myProgress2.setProgress(0); // 通过线程来改变Progress的值 new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { try { intCounter = (i + 1) * 20; Thread.sleep(1000); if (i == 4) { Message m = new Message(); m.what = ProgressBarActivity.GUI_STOP_NOTIFIER; ProgressBarActivity.this.myMessageHandler .sendMessage(m); break; } else { Message m = new Message(); m.what = ProgressBarActivity.GUI_THREADING_NOTIFIER; ProgressBarActivity.this.myMessageHandler .sendMessage(m); } } catch (Exception e) { e.printStackTrace(); } } } }).start(); } }); } Handler myMessageHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case ProgressBarActivity.GUI_STOP_NOTIFIER: // myProgress1.setVisibility(View.GONE); // myProgress2.setVisibility(View.GONE); // Thread.currentThread().interrupt(); break; case ProgressBarActivity.GUI_THREADING_NOTIFIER: if (!Thread.currentThread().isInterrupted()) { myProgress1.setProgress(intCounter); myProgress1.setProgress(intCounter); setProgress(intCounter * 100);// 设置标题栏中前景的一个进度条进度值 // setSecondaryProgress(intCounter*100);//设置标题栏中后面的一个进度条进度值 // Thread.currentThread().interrupt(); } break; } super.handleMessage(msg); } }; }