Android 控件使用示例

awpcch2007 贡献于2013-04-25

作者 shuyong  创建于2013-02-25 08:41:00   修改者张可  修改于2013-02-25 08:41:00字数48590

文档摘要:Android 控件使用示例
关键词:

Android控件 在 Android 中使用各种控件(View) ? TextView - 文本显示控件 ? Button - 按钮控件 ? ImageButton - 图片按钮控件 ? ImageView - 图片显示控件 ? CheckBox - 复选框控件 ? RadioButton - 单选框控件 ? AnalogClock - 钟表(带表盘的那种)控件 ? DigitalClock - 电子表控件 1、TextView(文本显示控件) 的 Demo textview.xml 代码 _TextView.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class _TextView extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.textview); // 设置 Activity 的标题 setTitle("TextView"); TextView txt = (TextView) this.findViewById(R.id.textView); // 设置文本显示控件的文本内容,需要换行的话就用“\n” txt.setText("我是 TextView\n显示文字用的"); } } 2、Button(按钮控件) 的 Demo button.xml 代码 _Button.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class _Button extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.button); setTitle("Button"); Button btn = (Button) this.findViewById(R.id.button); btn.setText("click me"); // setOnClickListener() - 响应按钮的鼠标单击事件 btn.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { TextView txt = (TextView) _Button.this.findViewById(R.id.textView); txt.setText("按钮被单击了"); } }); } } 3、ImageButton(图片按钮控件) 的 Demo imagebutton.xml 代码 _ImageButton.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.TextView; public class _ImageButton extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.imagebutton); setTitle("ImageButton"); ImageButton imgButton = (ImageButton) this.findViewById(R.id.imageButton); // 设置图片按钮的背景 imgButton.setBackgroundResource(R.drawable.icon01); // setOnClickListener() - 响应图片按钮的鼠标单击事件 imgButton.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { TextView txt = (TextView) _ImageButton.this.findViewById(R.id.textView); txt.setText("图片按钮被单击了"); } }); } } 4、ImageView(图片显示控件) 的 Demo imageview.xml 代码 _ImageView.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.widget.ImageView; public class _ImageView extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.imageview); setTitle("ImageView"); ImageView imgView = (ImageView) this.findViewById(R.id.imageView); // 指定需要显示的图片 imgView.setBackgroundResource(R.drawable.icon01); } } 5、CheckBox(复选框控件) 的 Demo checkbox.xml 代码 _CheckBox.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; public class _CheckBox extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.checkbox); setTitle("CheckBox"); CheckBox chk = (CheckBox) this.findViewById(R.id.chk1); // setOnCheckedChangeListener() - 响应复选框的选中状态改变事件 chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { TextView txt = (TextView) _CheckBox.this.findViewById(R.id.textView); txt.setText("CheckBox01 的选中状态:" + String.valueOf(isChecked)); } }); } } 6、RadioButton(单选框控件) 的 Demo radiobutton.xml 代码 _RadioButton.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; public class _RadioButton extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.radiobutton); setTitle("RadioButton"); RadioGroup group = (RadioGroup) this.findViewById(R.id.radioGroup); // setOnCheckedChangeListener() - 响应单选框组内的选中项发生变化时的事件 group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { TextView txt = (TextView) _RadioButton.this.findViewById(R.id.textView); txt.setText(((RadioButton)findViewById(checkedId)).getText() + " 被选中"); } }); } } 7、AnalogClock(钟表(带表盘的那种)控件) 的 Demo analogclock.xml 代码 _AnalogClock.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; public class _AnalogClock extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.analogclcok); setTitle("AnalogClock"); } } 运行例图: 8、DigitalClock(电子表控件) 的 Demo digitalclock.xml 代码 _DigitalClock.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; public class _DigitalClock extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.digitalclcok); setTitle("DigitalClcok"); } } ? DatePicker - 日期选择控件 ? TimePicker - 时间选择控件 ? ToggleButton - 双状态按钮控件 ? EditText - 可编辑文本控件 ? ProgressBar - 进度条控件 ? SeekBar - 可拖动的进度条控件 ? AutoCompleteTextView - 支持自动完成功能的可编辑文本控件 ? MultiAutoCompleteTextView - 支持自动完成功能的可编辑文本控件,允许输入多值(多值之间会自动地用指定的分隔符分开) 9、DatePicker (日期选择控件)的 Demo datepicker.xml 代码 _DatePicker.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; public class _DatePicker extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.datepicker); // 具体的应用可参见对话框中的示例 setTitle("DatePicker"); } } 10、TimePicker(时间选择控件) 的 Demo timepicker.xml 代码 _TimePicker.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; public class _TimePicker extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.timepicker); // 具体的应用可参见对话框中的示例 setTitle("TimePicker"); } } 11、ToggleButton(双状态按钮控件) 的 Demo togglebutton.xml 代码 _ToggleButton.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.ToggleButton; public class _ToggleButton extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.togglebutton); setTitle("ToggleButton"); final ToggleButton btn = (ToggleButton) this.findViewById(R.id.toggleButton); // setOnClickListener() - 响应按钮的鼠标单击事件 btn.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { TextView txt = (TextView) _ToggleButton.this.findViewById(R.id.textView); // ToggleButton.isChecked() - 双状态按钮的按钮状态 txt.setText("按钮状态:" + String.valueOf(btn.isChecked())); } }); } } 12、EditText(可编辑文本控件) 的 Demo edittext.xml 代码 _EditText.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.widget.EditText; public class _EditText extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.edittext); setTitle("EditText"); EditText txt = (EditText) this.findViewById(R.id.editText); txt.setText("我可编辑"); } } 13、ProgressBar(进度条控件) 的 Demo progressbar.xml 代码 _ProgressBar.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.view.Window; // 另见对话框中的进度条 public class _ProgressBar extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // 设置特性以允许在应用程序的标题栏上显示进度条(条状) 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); } } 例图: 14、SeekBar(可拖动的进度条控件) 的 Demo seekbar.xml 代码 _SeekBar.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.widget.SeekBar; import android.widget.TextView; public class _SeekBar extends Activity implements SeekBar.OnSeekBarChangeListener { SeekBar mSeekBar; TextView mProgressText; TextView mTrackingText; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub 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); } // 拖动进度条后,进度发生改变时的回调事件 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { mProgressText.setText(progress + "%"); } // 拖动进度条前开始跟踪触摸 public void onStartTrackingTouch(SeekBar seekBar) { mTrackingText.setText("开始跟踪触摸"); } // 拖动进度条后停止跟踪触摸 public void onStopTrackingTouch(SeekBar seekBar) { mTrackingText.setText("停止跟踪触摸"); } } 例图: 15、AutoCompleteTextView(支持自动完成功能的可编辑文本控件) 的 Demo autocompletetextview.xml 代码 _AutoCompleteTextView.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class _AutoCompleteTextView extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.autocompletetextview); setTitle("AutoCompleteTextView"); // 实例化适配器,指定显示格式及数据源 ArrayAdapter adapter = new ArrayAdapter( this, android.R.layout.simple_dropdown_item_1line, ary); AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.editText); // 指定自动完成控件的适配器 textView.setAdapter(adapter); } // 自动完成控件的所需数据的数据源 private String[] ary = new String[] { "abc", "abcd", "abcde", "abcdef", "abcdefg", "hij", "hijk", "hijkl", "hijklm", "hijklmn", }; } 例图: 16、MultiAutoCompleteTextView(支持自动完成功能的可编辑文本控件,允许输入多值【多值之间会自动地用指定的分隔符分开】) 的 Demo multiautocompletetextview.xml 代码 _MultiAutoCompleteTextView.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.MultiAutoCompleteTextView; public class _MultiAutoCompleteTextView extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.multiautocompletetextview); setTitle("MultiAutoCompleteTextView"); // 实例化适配器,指定显示格式及数据源 ArrayAdapter adapter = new ArrayAdapter( this, android.R.layout.simple_dropdown_item_1line, ary); MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.editText); textView.setAdapter(adapter); // 设置多个值之间的分隔符,此处为逗号 textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); } // 自动完成控件的所需数据的数据源 private String[] ary = new String[] { "abc", "abcd", "abcde", "abcdef", "abcdefg", "hij", "hijk", "hijkl", "hijklm", "hijklmn", }; } ? ZoomControls - 放大/缩小按钮控件 ? Include - 整合控件 ? VideoView - 视频播放控件 ? WebView - 浏览器控件 ? RatingBar - 评分控件 ? Tab - 选项卡控件 ? Spinner - 下拉框控件 ? Chronometer - 计时器控件 ? ScrollView - 滚动条控件 17、ZoomControls(放大/缩小按钮控件) 的 Demo zoomcontrols.xml 代码 _ZoomControls.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; import android.widget.ZoomControls; public class _ZoomControls extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.zoomcontrols); setTitle("ZoomControls"); ZoomControls zoomControls = (ZoomControls) this.findViewById(R.id.zoomControls); // setOnZoomInClickListener() - 响应单击放大按钮的事件 zoomControls.setOnZoomInClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(_ZoomControls.this, "单击了放大按钮", Toast.LENGTH_SHORT).show(); } }); // setOnZoomOutClickListener() - 响应单击缩小按钮的事件 zoomControls.setOnZoomOutClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(_ZoomControls.this, "单击了缩小按钮", Toast.LENGTH_SHORT).show(); } }); } } 例图: 18、Include(整合控件) 的 Demo include.xml 代码 include_1.xml 代码 include_2.xml 代码 _Include.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; public class _Include extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.include); setTitle("Include"); } } 19、VideoView(视频播放控件) 的 Demo videoview.xml 代码 _VideoView.java 代码 package com.webabcd.view; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.widget.MediaController; import android.widget.VideoView; public class _VideoView extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.videoview); setTitle("VideoView"); VideoView videoView = (VideoView) findViewById(R.id.videoView); // 指定需要播放的视频的地址 videoView.setVideoURI(Uri.parse("android.resource://com.webabcd.view/" + R.raw.demo)); // videoView.setVideoPath(); // 设置播放器的控制条 videoView.setMediaController(new MediaController(this)); // 开始播放视频 videoView.start(); } } 20、WebView(浏览器控件) 的 Demo webview.xml 代码 _WebView.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; public class _WebView extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.webview); setTitle("WebView"); WebView webView = (WebView) findViewById(R.id.webView); // 配置浏览器,使其可支持 JavaScript WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); // 清除浏览器缓存 webView.clearCache(true); // 指定浏览器需要解析的 url 地址 webView.loadUrl("http://webabcd.cnblogs.com/"); // 指定浏览器需要解析的 html 数据 // webView.loadData("webabcd", "text/html", "utf-8"); } } 21、RatingBar(评分控件) 的 Demo ratingbar.xml 代码 _RatingBar.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.widget.RatingBar; import android.widget.TextView; public class _RatingBar extends Activity implements RatingBar.OnRatingBarChangeListener { private RatingBar mRatingBar; private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.ratingbar); setTitle("RatingBar"); mTextView = (TextView) findViewById(R.id.textView); mRatingBar = (RatingBar) findViewById(R.id.ratingBar); // setOnRatingBarChangeListener() - 响应评分值发生改变的事件 mRatingBar.setOnRatingBarChangeListener(this); } @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { mTextView.setText(String.valueOf(rating)); } } 例图: 22、Tab(选项卡控件) 的 Demo tab.xml 代码 _Tab.java 代码 package com.webabcd.view; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; // 实现 Tab 功能的话要继承 TabActivity public class _Tab extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); TabHost tabHost = getTabHost(); LayoutInflater.from(this).inflate(R.layout.tab, tabHost.getTabContentView(), true); // Tab 1 的内容 tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1") .setContent(R.id.view1)); // Tab 2 的内容(设置了 Tab 图片) tabHost.addTab(tabHost.newTabSpec("tab2") .setIndicator("tab2", getResources().getDrawable(R.drawable.icon01)) .setContent(R.id.view2)); // Tab 3 的内容(设置 Tab 的内容为指定的 Activity) tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator("tab3") .setContent(new Intent(this, _TextView.class))); } } 23、Spinner(下拉框控件) 的 Demo spinner.xml 代码 Values/select.xml 代码 red blue green yellow black _Spinner.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; public class _Spinner extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.spinner); setTitle("Spinner"); Spinner spinner = (Spinner) findViewById(R.id.spinner); // 设置下拉框控件的标题文本 spinner.setPrompt("请选择"); // 实例化适配器,指定显示格式及数据源 ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.colors, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); // setOnItemSelectedListener() - 响应下拉框的选中值发生变化的事件 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { TextView textView = (TextView)_Spinner.this.findViewById(R.id.textView); textView.setText(((TextView)arg1).getText()); } @Override public void onNothingSelected(AdapterView arg0) { } }); } } 24、Chronometer(计时器控件) 的 Demo chronometer.xml 代码 _Chronometer.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class _Chronometer extends Activity { private Chronometer mChronometer; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.chronometer); setTitle("Chronometer"); Button button; mChronometer = (Chronometer) findViewById(R.id.chronometer); // 设置计时器所显示的时间格式 mChronometer.setFormat("计时:(%s)"); button = (Button) findViewById(R.id.btnStart); button.setOnClickListener(mStartListener); button = (Button) findViewById(R.id.btnStop); button.setOnClickListener(mStopListener); button = (Button) findViewById(R.id.btnReset); button.setOnClickListener(mResetListener); } View.OnClickListener mStartListener = new OnClickListener() { public void onClick(View v) { // 启动计时器 mChronometer.start(); } }; View.OnClickListener mStopListener = new OnClickListener() { public void onClick(View v) { // 暂停计时器 mChronometer.stop(); } }; View.OnClickListener mResetListener = new OnClickListener() { public void onClick(View v) { // 复位计时器,即停止计时器 mChronometer.setBase(SystemClock.elapsedRealtime()); } }; } 25、ScrollView(滚动条控件) 的 Demo scrollview.xml 代码 _ScrollView.java 代码 package com.webabcd.view; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class _ScrollView extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.scrollview); setTitle("ScrollView"); TextView textView = (TextView)this.findViewById(R.id.textView); textView.setText("a\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na"); } } 例图: ? TextSwitcher - 文字转换器控件(改变文字时增加一些动画效果) ? Gallery - 缩略图浏览器控件 ? ImageSwitcher - 图片转换器控件(改变图片时增加一些动画效果) ? GridView - 网格控件 ? ListView - 列表控件 ? ExpandableList - 支持展开/收缩功能的列表控件 26、TextSwitcher(文字转换器控件(改变文字时增加一些动画效果)) 的 Demo textswitcher.xml 代码

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 8 金币 [ 分享文档获得金币 ] 2 人已下载

下载文档