Android—开发自学历程(1)-Activity(4)-UI控件
2015-07-25 17:39
423 查看
一款软件最重要的是什么?不同的人有不同的理解。对于程序员来说,如何实现一个功能而且不出bug才是重点,然而对于用户来说,功能对于他们而言他不应该是基本么,他们有时更加看重一款软件的界面,因为如果一款软件功能非常强大,但是界面丑的爆,想必也不会有人用吧。所以开发程序UI设计也是非常非常重要的。所以接下来就来具体了解android几种常见的布局和组件!
这里我们可以看见几个属性,android:id=“”这是给这个控件设置一个唯一标识符(注意@+id/这样才是增加标识符);android:layout_width=”…”、android:layout_height=”…”分别为设置宽高,里面有三个可选值match_parent、fill_parent和wrap_content,其中match_parent等同于fill_parent,但是官方推荐使用match_parent;match_parent表示与当前父容器大小一致,wrap_content表示大小刚好可以包裹住容器内中的内容。android:text=”…”表示显示当前文本内容。
控件的属性大体上跟TextView,这里就是有个hint这个是提示信息,提示用户这个是干嘛的,当用户输入东西后就会消失;不过随着输入的内容不断增多,EditText会被不断地拉长,这时我们就可以设置maxlines,maxlines设置2行的话,我们最多拉长两行,剩下的就会往上滚,这样EditText就不会被拉长了。
但是单纯的在xml定义按钮是没有什么功能的,我们还必须在java源码中给它加个监听器
这样每当我们点击按钮时,都会触发一个onClick()方法,我们只要在里面添加点处理事件的逻辑就行了。监听的方法有很多,这里使用的是匿名类的方法。其他方法这里就不介绍。
布局文件
监听器
布局文件
监听器
上面这段代码有个src这里就是你的图片链接,通常我们开发android使用的图片后缀为.png,所以这里默认也是.png图片。这个放在你的res-drawable文件夹下。
布局文件看出跟ProgressBar设置挺像的,这里有个Progress这个的意思初始位置在50.
控件
1.4.1 TextView
一个简单直观的控件,通常用以显示一段文本信息<TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This is TextView" />
这里我们可以看见几个属性,android:id=“”这是给这个控件设置一个唯一标识符(注意@+id/这样才是增加标识符);android:layout_width=”…”、android:layout_height=”…”分别为设置宽高,里面有三个可选值match_parent、fill_parent和wrap_content,其中match_parent等同于fill_parent,但是官方推荐使用match_parent;match_parent表示与当前父容器大小一致,wrap_content表示大小刚好可以包裹住容器内中的内容。android:text=”…”表示显示当前文本内容。
1.4.2 EditText
android输入框,用户可以在上面编写和输入内容。<EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="提示内容" android:maxLines="2"/>
控件的属性大体上跟TextView,这里就是有个hint这个是提示信息,提示用户这个是干嘛的,当用户输入东西后就会消失;不过随着输入的内容不断增多,EditText会被不断地拉长,这时我们就可以设置maxlines,maxlines设置2行的话,我们最多拉长两行,剩下的就会往上滚,这样EditText就不会被拉长了。
1.4.3 Button
这个简单,就是一个按钮的意思。<Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" />
但是单纯的在xml定义按钮是没有什么功能的,我们还必须在java源码中给它加个监听器
public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 在此处添加逻辑 } }); } }
这样每当我们点击按钮时,都会触发一个onClick()方法,我们只要在里面添加点处理事件的逻辑就行了。监听的方法有很多,这里使用的是匿名类的方法。其他方法这里就不介绍。
1.4.4 RadioButton
单选按钮。布局文件
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioGroup android:id="@+id/radioGroup" android:contentDescription="性别" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radioMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:checked="true" />//默认选择 <RadioButton android:id="@+id/radioFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女"/> </RadioGroup> <TextView android:id="@+id/sex_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingLeft="50dp" android:text="您的性别是:男" android:textSize="9pt"/> </LinearLayout>
监听器
tv = (TextView) findViewById(R.id.sex_textview); //根据ID找到RadioGroup控件 group = (RadioGroup) findViewById(R.id.radioGroup); //绑定一个监听器 group.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //获取改变选项后的ID int id = group.getCheckedRadioButtonId(); //根据获取的ID创建对象 RadioButton rb = (RadioButton) findViewById(id); //更新文本。 tv.setText("您的性别是:" + rb.getText()); } });
1.4.5 CheckBox
复选框。布局文件
<CheckBox android:id="@+id/check_box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="选择它" android:checked="false"/>
监听器
checBox = (CheckBox) findViewById(R.id.check_box); checBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Toast.makeText(LinearLayoutActivity.this, "哇!你居然选择了", Toast.LENGTH_SHORT).show(); } });
1.4.6 ImageView
ImageView是用于在界面上展示图片的一个控件,通过它可以让我们的程序界面变得更加丰富多彩。<ImageView android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/>
上面这段代码有个src这里就是你的图片链接,通常我们开发android使用的图片后缀为.png,所以这里默认也是.png图片。这个放在你的res-drawable文件夹下。
1.4.7 ProgressBar
这是一个进度条,或者说是加载框,我在这里加了个样式style,这个的意思让它由原本的转圈变成一条直线,下面的max是这个进度条有多长。<ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:max="100" />
1.4.8 SeekBar
这个是ProgressBar的变种这个一个可以拖动的进度条,通常我们用来设置音乐播放器的进度条。布局文件看出跟ProgressBar设置挺像的,这里有个Progress这个的意思初始位置在50.
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/seek_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0"/> <SeekBar android:id="@+id/seek_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50"/> </LinearLayout>
mSeekBar = (SeekBar) findViewById(R.id.seek_bar); mSeeKView = (TextView) findViewById(R.id.seek_text); mSeeKView.setText("当前值:" + mSeekBar.getProgress()); mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { //停止拖动 mSeeKView.setText("最终值:" + mSeekBar.getProgress()); } @Override public void onStartTrackingTouch(SeekBar seekBar) { //开始拖动 mSeeKView.setText("开始值:" + mSeekBar.getProgress()); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { mSeeKView.setText("当前值:" + mSeekBar.getProgress()); } });
1.4.9 AlertDialog
这是一个弹窗,这个弹出来时,将会隐藏Activity,屏蔽其他控件,所以这个控件通常出现在执行一些重要紧急的事件,如你要删除某个东西。//AlertDialog //首先注册个按钮 mButton = (Button) findViewById(R.id.button_view); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //首先通过AlertDialog.Builder创建一个实例 AlertDialog.Builder dialog = new AlertDialog.Builder(LinearLayoutActivity.this); dialog.setTitle("This is AlertDialog")//标题 .setMessage("Something important.")//内容 .setCancelable(false);//不可点击其他地方取消 dialog.setPositiveButton("ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); dialog.show();//显示出来 } });
相关文章推荐
- SharePoint Error : System.ArgumentException. Value does not fall within the expected range
- error C2044: illegal continue!break 和continue的用法
- iOS开发-控件基类:UIView
- 工作队列 ( workqueue )
- UIButton的titleEdgeInsets和imageEdgeInsets使用技巧
- MIUI应用权限设置
- 【从零开始学NGUI 】 (九)分页效果实现
- 数组作为实参和形参的形式是怎样的?zhidao.baidu.com/link?url=owojlL0OUiAYU50L9g86kmo5AToWjv42ZQo9WN0HhtvHzGbcU7etRbJ69
- HDU 5301 Buildings 建公寓(逻辑,水)
- hdu5297 Y sequence 容斥加迭代
- UiAutomator——环境搭建及简单使用
- 通过回调实现Service中更新UI。
- CF Gym 100637K Microcircuits (DP)
- NGUI 3.5课程(五岁以下儿童)button-图片切换
- POJ 1679 The Unique MST
- ios学习整理(二)使用drawRect:方法自定义绘图和UIBezierPath类
- iOS开发-UITableView单选多选/复选实现1
- Ubuntu GUI工具及其对应命令
- Android—开发自学历程(1)-Activity(4)-UI布局
- hdu3530 Subsequence 单调队列