Android control Introduction
Android control IntroductionMulti-choice button (CheckBox)
CheckBox has two common events,OnClickListenerEvents andOnClickChangeListenerEvent
The effect is as follows:
Code:
Package com. example. z1178.test; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. menu; import android. view. menuItem; import android. view. view; import android. widget. checkBox; import android. widget. compoundButton; public class MainActivity extends Activity {private static final String TAG = debug; private CheckBox eat_checkBox; private CheckBox sleep_checkBox; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. linear_layout); eat_checkBox = (CheckBox) findViewById (R. id. eat_checkBox); sleep_checkBox = (CheckBox) findViewById (R. id. sleep_checkBox); OnCheckBoxClickListener listener1 = new OnCheckBoxClickListener (); CompoundButton. onCheckedChangeListener listener2 = new OnCheckedChangeListener (); // Bind the Click Event eat_checkBox.setOnClickListener (listener1); sleep_checkBox.setOnClickListener (listener1); // bind the status change event listener (listener2); listener (listener2 );} @ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. menu_main, menu); Return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); // noinspection SimplifiableIfStatement if (id = R. id. action_settings) {return true;} return super. onOptionsI TemSelected (item);} class OnCheckBoxClickListener implements View. onClickListener {@ Override public void onClick (View v) {CheckBox box = (CheckBox) v; int id = v. getId (); switch (id) {case R. id. eat_checkBox: Log. d (TAG, eat_checkBox is clicked!: + Box. isChecked (); break; case R. id. sleep_checkBox: Log. d (TAG, sleep_checkBox is clicked: + box. isChecked (); break ;}} class OnCheckedChangeListener implements CompoundButton. onCheckedChangeListener {@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {CheckBox box = (CheckBox) buttonView; Log. d (TAG, box. getText () + checkBox changed, its statue is + isChecked );}}}
To add the Select All button on the interface, add the corresponding control in xml, obtain the object in the code, and then bind the OnClickChangeListener event. The key code is as follows:
@Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { CheckBox box=(CheckBox)buttonView; int id=box.getId(); if(id!=R.id.checkall_checkBox){ Log.d(TAG,box.getText()+ checkBox changed ,its statue is +isChecked); }else{ eat_checkBox.setChecked(isChecked); sleep_checkBox.setChecked(isChecked); } }
Radio button (RadioButton)
The single-choice buttons include RadioButton and RadioGroup. Their events are the same as those in the CheckBox.OnClickListenerEvents andOnClickChangeListenerEvent.
ImageView
Configuration control:
You can also set the image in the code
imageView.setImageResource(R.drawable.layout_image);
ImageView is an ImageView object, and R. drawable. layout_image is an image in drawable.
ScaleType
ScaleType controls image attributes as follows:
| Attribute |
| CENTER |
| CENTER_CROP |
| CENTER_INSIDE |
| FIT_CENTER (START, END) |
| FITXY |
Effect
The width and height of the image are set to 100dp, scaleType is set to fitCenter, and the background color is set. The image scales proportionally according to the width and height, and the background color is filled in when there is not enough.
If one of the scaleType IDS is fitStart, the effect is as follows:
When scaleType is set to center:
When the image size is greater than the specified size, the image is cropped. If the size is smaller than the specified size, the image is displayed in the center. Note that there is no background color.
When scaleType is centerInside:
The difference between centerInside and centerFit is that when the image is too large, there is no difference between the two, but when the image is smaller than the specified size, centerFit will be enlarged to adapt to the size, while centerInside will not do this, only in the center. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPnNjYWxlVHlwZc6qY2VudGVyQ3JvcMqxo7o8L3A + DQo8cD48aW1nIGFsdD0 = "here write picture description" src = "http://www.bkjia.com/uploads/allimg/150724/042213J38-5.png" title = "\"/>
CenterCrop scales the shorter side of an image proportionally from the shorter side. If the longer side is too long, the image is truncated. Therefore, the image has no background color.
Of course, we can also scale the image in the code.
imageView.setScaleType(ScaleType.CENTER);
The parameter is of the enumeration type.
TimePicker and DatePicker
Both TimePicker and DatePicker have OnTimeChangedListener events.
Package com. example. z1178.test; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. menu; import android. view. menuItem; import android. widget. timePicker; public class MainActivity extends Activity {private static final String TAG = debug; private TimePicker timePicker; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. linear_layout); timePicker = (TimePicker) findViewById (R. id. timePicker); timePicker. setIs24HourView (true); // set the listener and bind the event timePickerChangedListener listener = new timePickerChangedListener (); timePicker. setOnTimeChangedListener (listener);} @ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. menu_main, menu); return true;} @ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); // noinspection SimplifiableIfStatement if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item);} class timePickerChangedListener implements TimePicker. onTimeChangedListener {@ Override public void onTimeChanged (TimePicker view, int hourOfDay, int minute) {Log. d (TAG, hourOfDay +: + minute );}}}
By default, the current system time is displayed. You can set the time in the Code. Also, you can set the listener through the buttons on the page to get the time. Note that the month starts from 0.
AnalogClock and DigitalClock will not be introduced here.
ProgressBar, SeekBar, and RatingBar
Properties of ProgressBar:
| Attribute 1 |
Attribute 2 |
Method |
| Horizontal) |
Judge whether it is a circular progress bar (isIndeterminated) |
Max progress) |
| Samll) |
Add progress (incrementProgressBy) |
Current progress (progress) |
| Large style (Large) |
Add the Second Progress (incrementSecondProgressBy) |
Second Progress (secondProgress) |
| Reverse style (Large. Inverse) |
|
|
| Inverse) |
|
|
| Small reverse style (Samll. Inverse) |
|
|
SeekBar listening event
| Event |
| OnProgressChanged (SeekBar seekBar, int progress, boolean fromUser) |
| OnStartTrackingTouch (SeekBar seekBar) |
| OnStopTrackingTouch (SeekBar seekBar) |
Attributes of RatingBar:
| Attribute |
Event |
| Number of Stars (numStars) |
OnRatingBarChanged (RatingBar ratingBar, float rating, boolean fromUser) |
| Current grade (progress) |
|
| StepSize |
|