Android开发中,Google工程师已经给咱们封装好了不少的按钮,使得咱们在开发中很是的方便和便捷。html
那么今天就来认识一下经常使用的按钮,那么在以前的课程中我已经详细讲过了Button按钮,那么这里就再也不重复了android
Android开发-之监听button点击事件:http://www.cnblogs.com/xiao-chuan/p/6074075.htmlweb
按钮的公共属性包括:1)经常使用的样式属性网络
a、background多线程
b、marginapp
c、……框架
2)宽高ide
a、widthpost
b、height学习
3)大小
a、size
b、max(min)
c、……
4)文本显示内容
a、text
b、hint
5)惟一键ID
a、id
6)点击事件
TextView:文本框
autoLink:文本的默认路径
<TextView android:layout_width="match_parent" android:layout_height="50sp" android:text="中国移动:10086" android:textSize="20sp" android:autoLink="phone" /> <TextView android:layout_width="match_parent" android:layout_height="50sp" android:text="站长邮箱:10086@qq.com" android:textSize="20sp" android:autoLink="email" /> <TextView android:layout_width="match_parent" android:layout_height="50sp" android:text="有事找百度:http://www.baidu.com" android:textSize="20sp" android:autoLink="web" />
那么在这里呢,若是点击第一个 TextView默认效果就是电话啦,那么第二个第三个呢~~一下是实现的效果图,这里你们能够看到我并无指定文本颜色,这里注意的是autoLink有默认的颜色啦!
EditText输入框
inputType:输入框输入的文本格式
<EditText android:id="@+id/userName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" android:textColor="@android:color/holo_red_light" android:textSize="20sp" android:inputType="text" android:maxLength="12" /> <EditText android:id="@+id/userAge" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入年龄" android:maxLength="3" android:numeric="integer" android:inputType="number" /> <EditText android:id="@+id/userPwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:maxLength="12" android:password="true" android:inputType="textPassword" /> <EditText android:id="@+id/userAddress" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入详细地址" android:maxLength="500" android:lines="3" android:inputType="textMultiLine" />
inputType:
text:文本格式
textPassword:密码格式
number:数字格式
textMultiLink:地址栏格式
……
注:那么你输入的时候呢输入法也会自动切换输入的方式,以密码格式输入那么输入的内容是不可见的。
这里分为:
1)SeekBar:调度,就好比咱们的音量,亮度等
2)RatingBar:选择,常见到的就是在电商网站上面的评论等
3)ProgressBar:加载、下载,加载图片,下载文件等
<TextView android:id="@+id/showText" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是一个TextView" android:textSize="20sp" /> <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="50" android:progress="20" /> <RatingBar android:id="@+id/ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:rating="1" android:stepSize="1" /> <ProgressBar android:id="@+id/progressBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="10" /> <ProgressBar android:id="@+id/progressBar2" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="10" style="?android:attr/progressBarStyleLarge" /> <ProgressBar android:id="@+id/progressBar3" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="10" style="?android:attr/progressBarStyleHorizontal" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="开始" android:onClick="doStart" /> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="结束" android:onClick="doStop" /> </LinearLayout>
页面效果:
如下是为了让你们更加清楚它们的效果和区别:
private SeekBar sb; private TextView showText; private RatingBar rb; private ProgressBar pb3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_seek_bars); showText=(TextView)findViewById(R.id.showText); sb=(SeekBar)findViewById(R.id.seekBar); rb=(RatingBar)findViewById(R.id.ratingBar); pb3=(ProgressBar)findViewById(R.id.progressBar3); //为seekBar绑定事件 sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { //改变showText字体大小 showText.setTextSize(progress); } }); rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { Toast.makeText(SeekBarsActivity.this, "你选择了"+rating+"星", 3000).show(); } }); } //开始下载[注意:除了ProgressBar外,全部的UI都必须在UI主线程中操做] boolean isRun=true; public void doStart(View view) throws Exception{ isRun=true; //构建一个执行进度条变化的子线程 new Thread(new Runnable() { @Override public void run() { //耗时操做不能放在主线程中执行 while(isRun){ if(pb3.getProgress()<100){ pb3.setProgress(pb3.getProgress()+1); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } }else{ isRun=false; //最简单方法实如今多线程更新UI runOnUiThread(new Runnable() { public void run() { Toast.makeText(SeekBarsActivity.this, "下载完毕",3000).show(); } }); } } } }).start(); } //结束下载 public void doStop(View view){ isRun=false; }
注意:
1)除了ProgressBar外,全部的UI都必须在UI主线程中操做,不然会报错
2)耗时操做不能放在主线程中执行,不然会报错
3)Google工程师让Android4.0之后的版本都不支持以上两点了,那么有人就要纠结了,那位了维护低版本,要怎么办~~本身想,很简单的问题!
1)imageButton:图片按钮
2)imageView:图片视图
imageView与HTML5对比:
imageView:运行更流畅,在没有网络的状况下也可使用。。
HTML5:运行时不够流畅,没有网就废了……可是优势在于页面更加美观,数据传输更加便捷。。
3)RadioButton:单选框,各元素是互斥的,只能选择一个,好比性别
4)CheckBox:多选按钮,能够选择多个,好比爱好
5)ToggleButton:单个提示按钮,好比开关
<RadioGroup android:id="@+id/rgsex" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/sexOne" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:checked="true" /> <RadioButton android:id="@+id/sexTwo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/cb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="篮球" /> <CheckBox android:id="@+id/cb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="足球" /> <CheckBox android:id="@+id/cb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="羽毛球" /> </LinearLayout> <ToggleButton android:id="@+id/stateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="开灯" android:textOff="关灯" android:checked="true" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/image001" android:scaleType="fitXY" android:maxWidth="120dp" android:maxHeight="60dp" android:adjustViewBounds="true" android:onClick="getValues" />
效果以下:
ps:图片打码了是由于实在找不到合适的图片而后又不想给人家打广告又不得钱……
如下一样为了更加清楚它们的效果和区别:
private RadioGroup rg; private CheckBox cb1,cb2,cb3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_buttons); cb1=(CheckBox)findViewById(R.id.cb1); cb2=(CheckBox)findViewById(R.id.cb2); cb3=(CheckBox)findViewById(R.id.cb3); rg=(RadioGroup)findViewById(R.id.rgsex); //监听单选按钮组事件 rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rb=(RadioButton)findViewById(checkedId); Toast.makeText(ButtonsActivity.this, "你选择了:"+rb.getText().toString(), 3000).show(); } }); } //取值[单选按钮|复选框] public void getValues(View view){ //单选按钮 RadioButton rb=(RadioButton)findViewById(rg.getCheckedRadioButtonId()); StringBuffer sb=new StringBuffer(); sb.append("性别:"+rb.getText().toString()+"\n"); //复选框 sb.append("爱好:"); if(cb1.isChecked()) sb.append(cb1.getText().toString()+","); if(cb2.isChecked()) sb.append(cb2.getText().toString()+","); if(cb3.isChecked()) sb.append(cb3.getText().toString()+","); Toast.makeText(this, sb.toString(), 5000).show(); }
PS:Google工程师都给咱们封装好了,咱们能够直接使用。咱们也能够本身写一个底层的框架去实现这些按钮,相信你们在学习Java的时候这些按钮的实现都已经本身有写过了,其实Google工程师所封装的底层代码也是那样子实现的。只是说~谁那么无聊啊现成的不用!可是若是你们之后作Android框架开发的时候……就须要本身写了~在基础知识更新完了之后呢……就会涉及到比较高级的内容了哈哈哈哈……彻底尚未准备!
一、其实还有不少经常使用的以及一些不经常使用的,无论怎样都但愿你们可以养成自学的习惯……到了公司更是如此!
二、Android框架的开发单纯的就是Java知识,因此跟Android开发没什么关系,可是又要对Android有很高的认知!
三、能够在Android页面中嵌套,可是要区分HTML5与Android palette之间的区别!
四、TextView和EditView的区别,其实很大……
五、palette要与相应的事件和业务逻辑一块儿使用才会真正的有意义,好比数据的传输~~在之后的课程中我会详细的讲解到。