【网学网提醒】:以下是网学会员为您推荐的Android 控件,希望本篇文章对您学习有所帮助。
love2010sky(
网学)贡献
介绍在Android中使用各种控件(View)TextView-文本显示控件Button-按钮控件ImageButton-图片按钮控件ImageView-
图片显示控件CheckBox-复选框控件RadioButton-单选框控件AnalogClock-钟表(带表盘的那种)控件DigitalClock-电子表控件
1,TextView的Demotextview.xml代码
_TextView.java代码packagecom.webabcd.view;importandroid.app.Activity;importandroid.os.Bundle;importandroid.widget.TextView;publicclass_TextViewextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){
//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.textview);//设置Activity的标题setTitle("TextView");TextViewtxt=(TextView)this.findViewById(R.id.textView);//设置文本显示控件的文本内容,需要换行的话就用""txt.setText("我是TextView显示文字用的");}}2,Button的Demobutton.xml代码
_Button.java代码packagecom.webabcd.view;
importimportimportimportimport
android.app.Activity;android.os.Bundle;android.view.View;android.widget.Button;android.widget.TextView;
publicclass_ButtonextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.button);setTitle("Button");Buttonbtn=(Button)this.findViewById(R.id.button);btn.setText("clickme");//setOnClickListener()-响应按钮的鼠标单击事件btn.setOnClickListener(newButton.OnClickListener(){@OverridepublicvoidonClick(Viewv){TextViewtxt=(TextView)_Button.this.findViewById(R.id.textView);txt.setText("按钮被单击了");}});}}3,ImageButton的Demoimagebutton.xml代码
_ImageButton.java代码packagecom.webabcd.view;importimportimportimportimportimportandroid.app.Activity;android.os.Bundle;android.view.View;android.widget.Button;android.widget.ImageButton;android.widget.TextView;
publicclass_ImageButtonextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.image
button);setTitle("ImageButton");ImageButtonimgButton=(ImageButton)this.findViewById(R.id.imageButton);//设置图片按钮的背景imgButton.setBackgroundResource(R.drawable.icon01);//setOnClickListener()-响应图片按钮的鼠标单击事件imgButton.setOnClickListener(newButton.OnClickListener(){@OverridepublicvoidonClick(Viewv){TextViewtxt=(TextView)_ImageButton.this.findViewById(R.id.textView);txt.setText("
图片按钮被单击了");
}});}}4,ImageView的Demoimageview.xml代码
_ImageView.java代码packagecom.webabcd.view;importandroid.app.Activity;importandroid.os.Bundle;importandroid.widget.ImageView;publicclass_ImageViewextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.imageview);setTitle("ImageView");ImageViewimgView=(ImageView)
this.findViewById(R.id.imageView);//指定需要显示的
图片imgView.setBackgroundResource(R.drawable.icon01);}}5,CheckBox的Democheckbox.xml代码
_CheckBox.java代码packagecom.webabcd.view;importimportimportimportimportandroid.app.Activity;android.os.Bundle;android.widget.CheckBox;android.widget.CompoundButton;android.widget.TextView;
publicclass_CheckBoxextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.checkbox);setTitle("CheckBox");CheckBoxchk=(CheckBox)this.findViewById(R.id.chk1);//setOnCheckedChangeListener()-响应复选框的选中状态改变事件chk.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){@OverridepublicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){TextViewtxt=(TextView)_CheckBox.this.findViewById(R.id.textView);txt.setText("CheckBox01的选中状态:"+String.valueOf(isChecked));}});}}6,RadioButton的Demoradiobutton.xml
代码
分组checkedButton-指定组内被选中的单选框的ID--> android:checkedButton="@+id/rad3"android:orientation="horizontal"android:gravity="center_vertical|center_horizontal">
_RadioButton.java代码packagecom.webabcd.view;importimportimportimportimportandroid.app.Activity;android.os.Bundle;android.widget.RadioButton;android.widget.RadioGroup;android.widget.TextView;
publicclass_RadioButtonextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.radiobutton);setTitle("RadioButton");RadioGroupgroup=(RadioGroup)this.findViewById(R.id.radioGroup);//setOnCheckedChangeListener()-响应单选框组内的选中项发生变化时的事件group.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener(){@OverridepublicvoidonCheckedChanged(RadioGroupgroup,int
checkedId){TextViewtxt=(TextView)_RadioButton.this.findViewById(R.id.textView);txt.setText(((RadioButton)findViewById(checkedId)).getText()+"被选中");}});}}7,AnalogClock的Demoanalogclock.xml代码
_AnalogClock.java代码packagecom.webabcd.view;importandroid.app.Activity;importandroid.os.Bundle;publicclass_AnalogClockextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstub
super.onCreate(savedInstanceState);this.setContentView(R.layout.analogclcok);setTitle("AnalogClock");}}8,DigitalClock的Demodigitalclock.xml代码
_DigitalClock.java代码packagecom.webabcd.view;importandroid.app.Activity;importandroid.os.Bundle;publicclass_DigitalClockextendsActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){//TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.digitalclcok);
setTitle("DigitalClcok");}}
1