方法一
Android 1.0
1 2 3 4 5 6 7
| mybutton = (Button) findViewById(R.id.button1); mybutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myedit.setTextColor(Color.YELLOW); } });
|
方法二
Android 1.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class XXXXX ... implements View.OnClickListener{ ... mybutton = (Button) findViewById(R.id.button1); mybutton.setOnClickListener(this); } public void onClick(View v){ switch (v.getId()){ case R.id.button1: myedit.setTextColor(Color.RED); break; default: break; } }
|
方法三
Android 1.6
android:onClick="myClickHandler"
1 2 3 4 5 6 7 8 9
| public void myClickHandler(View v){ switch (v.getId()){ case R.id.button2: myedit.setTextColor(Color.BLACK); break; default: break; } }
|
Image
Bitmap
1 2
| Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.koala); myimagebutton.setImageBitmap(icon);
|
Resource
1
| myimagebutton.setImageResource(R.drawable.penguins);
|
CheckBox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| mycheckbox = (CheckBox) findViewById(R.id.checkBox); mycheckbox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CheckBox t1 = (CheckBox) v; if (t1.isChecked()){ mycheckbox.setText("Checked"); Toast.makeText(MainActivity.this,"選",Toast.LENGTH_LONG).show(); }else{ mycheckbox.setText("Not Checked"); Toast.makeText(MainActivity.this,"不選",Toast.LENGTH_LONG).show(); } } }); 也可以直接if(mycheckbox.isChecked())
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| mygroup = (RadioGroup) findViewById(R.id.myRadioGroup); myradio = (RadioButton) findViewById(R.id.radioButton); myradio2 = (RadioButton) findViewById(R.id.radioButton2); mygroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { if (i == myradio.getId()){ myedit.setText("AA Apple"); } else if(i == myradio2.getId()){ myedit.setText("BB Banana"); } } });
|
Spinner 下拉式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| private String[] myStringArray = { "Dog", "Cat", "Bird"}; private Spinner mysp; ... mysp = (Spinner) findViewById(R.id.spinner); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myStringArray); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); mysp.setAdapter(adapter); mysp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { myedit.setText(myStringArray[i]); } @Override public void onNothingSelected(AdapterView<?> adapterView) { myedit.setText("QQ"); } });
|
NumberPicker
1 2 3 4 5 6 7 8 9 10 11 12 13
| private String[] nums = new String[21]; private NumberPicker mynp; ... for(int i=0; i<nums.length;i++){ nums[i] = Integer.toString(i*5); } mynp = (NumberPicker) findViewById(R.id.numberPicker); mynp.setMaxValue(nums.length-1); mynp.setMinValue(0); mynp.setWrapSelectorWheel(false); mynp.setDisplayedValues(nums); ... Toast.makeText(getApplicationContext(), String.valueOf(mynp.getValue()), Toast.LENGTH_SHORT).show();
|
點擊空白關閉鍵盤
1 2 3 4 5 6 7 8 9
| RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1); layout1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myedit.getWindowToken(),0); } });
|