android UI的使用.docx
- 文档编号:78784
- 上传时间:2023-04-28
- 格式:DOCX
- 页数:32
- 大小:277.54KB
android UI的使用.docx
《android UI的使用.docx》由会员分享,可在线阅读,更多相关《android UI的使用.docx(32页珍藏版)》请在冰点文库上搜索。
androidUI的使用
UI编程
View应用
1.Button:
按钮单击事件
Buttonbutton=(Button)this.findViewById(R.id.imageButton);
//设置图片按钮的背景
button.setBackgroundResource(R.drawable.icon01);
//设置背景颜色
//b.setBackgroundColor(color);
//b.setText(text);
//setOnClickListener()-响应图片按钮的鼠标单击事件
button.setOnClickListener(newButton.OnClickListener(){
@Override
publicvoidonClick(Viewv){
TextViewtxt=(TextView)_Button.this.findViewById(R.id.textView);
txt.setText("按钮被单击了");
}
});
2.EditText:
编辑框
3.TextView:
显示文本
4.ImageButton:
图片按钮:
用法和Button相似
5.ImageView:
图片显示控件
--ImageView-图片显示控件-->
id="@+id/imageView"android: layout_width="wrap_content" android: layout_height="wrap_content"> @Override protectedvoidonCreate(BundlesavedInstanceState){ //TODOAuto-generatedmethodstub super.onCreate(savedInstanceState); this.setContentView(R.layout.imageview); setTitle("ImageView"); ImageViewimgView=(ImageView)this.findViewById(R.id.imageView); //指定需要显示的图片 imgView.setBackgroundResource(R.drawable.icon01); } 6.CheckBox: 复选框 7.RadioButton: 单选按钮 8.AnalogClock: 钟表(带表盘的那种)控件 @Override protectedvoidonCreate(BundlesavedInstanceState){ //TODOAuto-generatedmethodstubsuper.onCreate(savedInstanceState); this.setContentView(R.layout.analogclcok) setTitle("AnalogClock"); } --AnalogClock-钟表(带表盘的那种)控件--> id="@+id/analogClock" android: layout_width="wrap_content"android: layout_height="wrap_content"> 9.DigitalClock电子表控件 --DigitalClock-电子表控件--> id="@+id/digitalClock" android: layout_width="wrap_content"android: layout_height="wrap_content"> @Override protectedvoidonCreate(BundlesavedInstanceState){ //TODOAuto-generatedmethodstub super.onCreate(savedInstanceState); this.setContentView(R.layout.digitalclcok); setTitle("DigitalClcok"); } 10.DatePicker日期选择控件 --DatePicker-日期选择控件--> id="@+id/datePicker" android: layout_width="wrap_content" android: layout_height="wrap_content"/> @Override protectedvoidonCreate(BundlesavedInstanceState){ //TODOAuto-generatedmethodstub super.onCreate(savedInstanceState); this.setContentView(R.layout.datepicker); //具体的应用可参见对话框中的示例 setTitle("DatePicker"); } 11.TimePicker时间选择控件 --TimePicker-时间选择控件--> android: id="@+id/timePicker" android: layout_width="wrap_content" android: layout_height="wrap_content"> @Override protectedvoidonCreate(BundlesavedInstanceState){ //TODOAuto-generatedmethodstub super.onCreate(savedInstanceState); this.setContentView(R.layout.timepicker); //具体的应用可参见对话框中的示例 setTitle("TimePicker"); } 12.ToggleButton双状态按钮控件 --ToggleButton-双状态按钮控件 textOn-当按钮状态为true时所显示的文本 textOff-当按钮状态为false时所显示的文本 --> id="@+id/toggleButton" android: layout_width="wrap_content" android: layout_height="wrap_content" android: textOn="关闭"android: textOff="打开"/> setTitle("ToggleButton"); finalToggleButtonbtn=(ToggleButton)this.findViewById(R.id.toggleButton); //setOnClickListener()-响应按钮的鼠标单击事件 btn.setOnClickListener(newButton.OnClickListener(){ @Override publicvoidonClick(Viewv){ TextViewtxt=(TextView)_ToggleButton.this.findViewById(R.id.textView); //ToggleButton.isChecked()-双状态按钮的按钮状态 txt.setText("按钮状态: "+String.valueOf(btn.isChecked())); } }); 13.ProgressBar进度条控件 //设置特性以允许在应用程序的标题栏上显示进度条(条状)requestWindowFeature(Window.FEATURE_PROGRESS); //设置特性以允许在应用程序的标题栏上显示进度条(圆圈状) requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); this.setContentView(R.layout.progressbar); setTitle("ProgressBar"); //在标题栏上显示进度条(条状) setProgressBarVisibility(true); //在标题栏上显示进度条(圆圈状) setProgressBarIndeterminateVisibility(true); //指定进度条的进度 setProgress(50*100); setSecondaryProgress(75*100); --ProgressBar-进度条控件--> --以下分别为大、中、小的进度条控件(圆圈状)--> id="@+android: id/progress_large" style="? android: attr/progressBarStyleLarge"android: layout_width="wrap_content" android: layout_height="wrap_content"/> id="@+android: id/progress" android: layout_width="wrap_content"android: layout_height="wrap_content"/> id="@+android: id/progress_small" style="? android: attr/progressBarStyleSmall"android: layout_width="wrap_content" android: layout_height="wrap_content"/> -- 进度条控件(条状)的演示 style-进度条的样式,本例使用内置样式 max-进度的最大值 progress-第一进度位置 secondaryProgress-第二进度位置 --> id="@+id/progress_horizontal" style="? android: attr/progressBarStyleHorizontal"android: layout_width="200px" android: layout_height="wrap_content"android: max="100" android: progress="50"android: secondaryProgress="75"/> 14.SeekBar: 可拖动的进度条控件 --SeekBar-可拖动的进度条控件 max-进度的最大值 progress-第一进度位置 secondaryProgress-第二进度位置--> id="@+id/seekBar" android: layout_width="fill_parent" android: layout_height="wrap_content"android: max="100" android: progress="50"android: secondaryProgress="75"/> id="@+id/progress"android: layout_width="fill_parent" android: layout_height="wrap_content"/> id="@+id/tracking"android: layout_width="fill_parent" android: layout_height="wrap_content"/> publicclass_SeekBarextendsActivityimplements SeekBar.OnSeekBarChangeListener{ SeekBarmSeekBar; TextViewmProgressText; TextViewmTrackingText; @Override protectedvoidonCreate(BundlesavedInstanceState){ //TODOAuto-generatedmethodstub super.onCreate(savedInstanceState); this.setContentView(R.layout.seekbar); setTitle("SeekBar"); mSeekBar=(SeekBar)findViewById(R.id.seekBar); //setOnSeekBarChangeListener()-响应拖动进度条事件 mSeekBar.setOnSeekBarChangeListener(this); mProgressText=(TextView)findViewById(R.id.progress); mTrackingText=(TextView)findViewById(R.id.tracking); } //拖动进度条后,进度发生改变时的回调事件 publicvoidonProgressChanged(SeekBarseekBar,intprogress, booleanfromTouch){ mProgressText.setText(progress+"%"); } //拖动进度条前开始跟踪触摸 publicvoidonStartTrackingTouch(SeekBarseekBar){ mTrackingText.setText("开始跟踪触摸"); } //拖动进度条后停止跟踪触摸 publicvoidonStopTrackingTouch(SeekBarseekBar){ mTrackingText.setText("停止跟踪触摸"); } } 15.AutoCompleteTextView-支持自动完成功能的可编辑文本控件 android: id="@+id/auto_edit" android: layout_width="wrap_content" android: layout_height="wrap_content" > publicclassTextextendsActivity{ AutoCompleteTextViewautoview; ArrayAdapter //defaultcity String[]city={ "shanghai","beijing","tianjing","wuhan","sichuan" }; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); intialView(); //default=2 autoview.setThreshold (1); Buttonbtn=(Button)findViewById(R.id.btnname); btn.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewv){ //TODOAuto-generatedmethodstub Stringstring=autoview.getText().toString(); adapter.add(string); } }); } publicvoidintialView(){ adapter=newArrayAdapter android.R.layout.simple_dropdown_item_1line,city); autoview=(AutoCompleteTextView)findViewById(R.id.auto_edit); autoview.setWidth(200); autoview.setAdapter(adapter); } } 16.MultiAutoCompleteTextView: 支持自动完成功能的可编辑文本控件 --MultiAutoCompleteTextView -支持自动完成功能的可编辑文本控件,允许输入多值(多值之间会自动地用指定的分隔符分开) --> id="@+id/editText" android: layout_width="fill_parent" android: layout_height="wrap_content"/> publicclass_MultiAutoCompleteTextViewextendsActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.multiautocompletetextview); setTitle("MultiAutoCompleteTextView"); //实例化适配器,指定显示格式及数据源 ArrayAdapter this, android.R.layout.simple_dropdown_item_1line, ary); MultiAutoCompleteTextViewtextView=(MultiAutoCompleteTextView)findViewById(R.id.editText); textView.setAdapter(adapter); //设置多个值之间的分隔符,此处为逗号 textView.setTokenizer(newMultiAutoCompleteTextView.CommaTokenizer()); } //自动完成控件的所需数据的数据源 privateString[]ary=newString[]{ "abc","abcd","abcde","abcdef","abcdefg", "hij","hijk","hijkl","hijklm","hijklmn",}; } 17.ZoomControls: 放大/缩小按钮控件 --放大/缩小按钮控件--> id="@+id/zoomControls" android: layout_width="wrap_content" android: layout_height="wrap_content"/> publicclass_ZoomControlsextendsActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ //TODOAuto-generatedmethodstub super.onCreate(savedInstanceState); this.setContentView(R.layout.zoomcontrols); setTitle("ZoomControls"); ZoomControlszoomControls=(ZoomControls)this.findViewById(R.id.zoomControls); //setOnZoomInClickListener()-响应单击放大按钮的事件 zoomControls.setOnZoomInClickListener(newOnClickListener(){ publicvoidonClick(Viewv){ Toast.makeText(_ZoomControls.this,"单击了放大按钮",Toast.LENGTH_SHORT).show(); } }); //setOnZoomOutClickListener()-响应单击缩小按钮的事件 zoomControls.setOnZoomOutClickListener(newOnClickListener(){ publicvoidonClick(Viewv){ Toast.makeText(_ZoomControls.this,"单击了缩小按钮",Toast.LENGTH_SHORT).show(); } }); }} 18.Include将指定的layout整合进来 -- include-将指定的la
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- android UI的使用 UI 使用