繼續Android開發的征程。
以下是Android應用開發揭秘這本書第四章的關於UI的事件處理的總結。基本上都是可以復用的代碼。存著日後方便。
1.EditView控件的setOnKeyListener方法。點擊上下左右鍵或者中間鍵的時候可以出發該方法。代碼如下。
- myEditView.setOnKeyListener(new EditText.OnKeyListener(){
-
- @Override
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- myTextView.setText("文本框中內容是:"+myEditView.getText().toString());
- return false;// TODO Auto-generated method stub
- }
- });
- myEditView.setOnKeyListener(new EditText.OnKeyListener(){
-
- @Override
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- myTextView.setText("文本框中內容是:"+myEditView.getText().toString());
- return false;// TODO Auto-generated method stub
- }
- });
2.RadioButton控件和RadioGroup控件的組合使用,代碼如下:
- myRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
-
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- // TODO Auto-generated method stub
- if(checkedId == myRadioButton3.getId())
- {
- DisplayToast("正確答案:"+myRadioButton3.getText()+"恭喜你回答正確");
- }
- else
- {
- DisplayToast("回答錯誤");
- }
- }
- );
- myRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
-
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- // TODO Auto-generated method stub
- if(checkedId == myRadioButton3.getId())
- {
- DisplayToast("正確答案:"+myRadioButton3.getText()+"恭喜你回答正確");
- }
- else
- {
- DisplayToast("回答錯誤");
- }
- }
- );
3.CheckBox控件的使用,代碼如下:
- myCheckBox1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- // TODO Auto-generated method stub
- if(myCheckBox1.isChecked()){
- DisplayToast("你選擇了:"+myCheckBox1.getText());
- }
- }
- });
- myCheckBox1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- // TODO Auto-generated method stub
- if(myCheckBox1.isChecked()){
- DisplayToast("你選擇了:"+myCheckBox1.getText());
- }
- }
- });
4.Button控件的使用,代碼如下:
- myButton1.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View v) {
- }
- });
- myButton1.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View v) {
- }
- });
4.Spinner控件的使用,代碼如下:
- //Spinner控件需要跟ArrayAdapter綁定使用。
- private static final String[] m_Countries = {"A型","B型","O型","AB型","未知類型"};
- private Spinner mySpinner;
- private ArrayAdapter<String> myAdapter;
- mySpinner = (Spinner)findViewById(R.id.mySpinner1);
- myAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m_Countries);
- //設置下拉列表風格
- myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- //將adapter添加到mySpinner中
- mySpinner.setAdapter(myAdapter);
- //添加事件監聽
- mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
- @Override
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- // TODO Auto-generated method stub
- myTextView.setText("你的血型是"+ m_Countries[arg2]);
- arg0.setVisibility(View.VISIBLE);
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- DisplayToast("你什麼也沒選!");
- }
- });
- //Spinner控件需要跟ArrayAdapter綁定使用。
- private static final String[] m_Countries = {"A型","B型","O型","AB型","未知類型"};
- private Spinner mySpinner;
- private ArrayAdapter<String> myAdapter;
- mySpinner = (Spinner)findViewById(R.id.mySpinner1);
- myAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m_Countries);
- //設置下拉列表風格
- myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
- //將adapter添加到mySpinner中
- mySpinner.setAdapter(myAdapter);
- //添加事件監聽
- mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
- @Override
- public void onItemSelected(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- // TODO Auto-generated method stub
- myTextView.setText("你的血型是"+ m_Countries[arg2]);
- arg0.setVisibility(View.VISIBLE);
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- DisplayToast("你什麼也沒選!");
- }
- });