Android常用控件
来源:互联网 发布:淘宝买活体狗靠谱吗 编辑:程序博客网 时间:2024/06/14 05:40
首先我们来看看androidd常用的控件有哪些
有两张图可以直接然我们了解安卓的常用控件
然后用以上的一些控件做简单的测试
一、制作一个登录的排版
效果就直接贴图
按钮我就没有去加了
说说这个布局所用到的控件以及属性吧
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="140dp" android:layout_height="50dp" android:drawableLeft="@drawable/icon_user" android:text="用户名:" android:textSize="30sp" android:gravity="center|right" /> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:paddingLeft="140dp" android:hint="请输入用户名" android:background="@drawable/test2" /> </FrameLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="140dp" android:layout_height="50dp" android:text="密码:" android:textSize="30sp" android:gravity="center|right" /> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:paddingLeft="140dp" android:hint="请输入密码" android:background="@drawable/test2" /> </FrameLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="140dp" android:layout_height="50dp" android:text="邮箱:" android:textSize="30sp" android:gravity="center|right" /> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:paddingLeft="140dp" android:hint="请输入邮箱" /> </FrameLayout></LinearLayout>
TextView是文本视图
android:drawableLeft是用户框左边的小图标,drawable后面接图标的位置
android:textSize是设置文字的大小
android:gravity=”center|right”表示使字体靠右据中,如果只写一个center或者right的话就表示只剧中或者靠右
EditText是文本编辑器
这里面没有用到太多的属性
android:hint是文字提示符(当用户输入内容后会消失不见)
外型资源以及选择器
1.外形资源shape
上面登录排版有用到外形资源以及选择器
首先要在deawable文件下新建一个Deawable resource file
命名看个人规范
将selector改成shape(selector是选择器shape是外形资源,我习惯先写外形资源)
这里我是调了两个样式属性
一个gradient表示渐变色
一个corners表示弧度(左上角的弧度)
再写一个选择器(selector),和上面一样的步骤,selector不用改
state_focused表示焦点状态,这里两行表示
当得到焦点时调用test外形资源,失去焦点时调用test1外形资源
然后直接调用这个选择器就好了
这里我设置背景颜色的时候掉用了我的选择器(test2)
二、排一个多选框和一个日期对话框
首先看看代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="看书" android:textSize="30sp" android:id="@+id/rb_main_honneya" android:checked="false" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="游戏" android:textSize="30sp" android:id="@+id/rb_main_honneyb" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="代码" android:textSize="30sp" android:id="@+id/rb_main_honneyc" android:checked="false" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" android:textSize="35dp" android:gravity="center" android:layout_gravity="center" android:onClick="sex" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="时间" android:textSize="35dp" android:gravity="center" android:layout_gravity="center" android:onClick="gettime" /></LinearLayout>
排版就不多说了效果如下
点击确定会弹出小窗口显示你选择了哪些选择
点击时间会弹出时间对话框
这个需要写函数,首先要找到写函数的地方
在你的项目里有个java文件夹,下面 的第一个文件里有一个MainActivity的java文件,我们的函数要在java文件里写
先来看看时间对话框怎么写
public void gettime(View view) { //获取当前系统时间 Calendar c = Calendar.getInstance(); new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { } }, c.get(Calendar.YEAR), c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show(); }
方法里面的calendar是用来获取系统当前时间
然后需要写一个时间的对话框(new DatePickerDialog)里面有5个参数,第一个写this,第二个参数需要new一个时间监听
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
} }
里面什么都不写,第三个参数写时间,用Calendar 去获取系统当前时间,从第三个开始分别写,年,月,最后一个时星期几,之后一定
要写.show(),不然运行不起来
写完这些后就可以调用这个方法了
怎么调用如图
在Button
里面onClick后面接方法名就可以了
效果就是这样的
再看看多选框
先看代码
public void sex(View view){ CheckBox rb_main_honneya = (CheckBox)findViewById(R.id.rb_main_honneya); CheckBox rb_main_honneyb = (CheckBox)findViewById( R.id.rb_main_honneyb); CheckBox rb_main_honneyc = (CheckBox)findViewById(R.id.rb_main_honneyc); String honney=""; if (rb_main_honneya.isChecked()){ honney += rb_main_honneya.getText().toString(); } if(rb_main_honneyb.isChecked()){ honney +=rb_main_honneyb.getText().toString(); } if (rb_main_honneyc.isChecked()){ honney += rb_main_honneyc.getText().toString();; } Toast to = Toast.makeText(getApplicationContext(), honney, Toast.LENGTH_LONG); to.setGravity(Gravity.CENTER, 0, 0); LinearLayout toastView = (LinearLayout) to.getView(); toastView.setOrientation(LinearLayout.HORIZONTAL); ImageView imageCodeProject = new ImageView(getApplicationContext()); imageCodeProject.setImageResource(R.drawable.icon_user); toastView.addView(imageCodeProject, 0); to.show(); }
rb_main_honneya = (CheckBox)findViewById(R.id.rb_main_honneya); rb_main_honneyb = (CheckBox)findViewById( R.id.rb_main_honneyb); rb_main_honneyc = (CheckBox)findViewById(R.id.rb_main_honneyc);
用来获取多选框id
设一个容器装内容,再判断每一个单选框是否选中,如果是则将该复选框的内容放到容器内。
最后设置弹窗(Toast )代码中的honney就是你想要弹出的内容
to.setGravity(Gravity.CENTER, 0, 0)是控制弹出的位置(CENTER为中间)
LinearLayout toastView = (LinearLayout) to.getView();
toastView.setOrientation(LinearLayout.HORIZONTAL);
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon_user);
上面加粗是弹窗里加图片,这个图片的路径
toastView.addView(imageCodeProject, 0);
效果如下
以上就是常用控件的简单应用
- android控件与常用控件
- android 常用控件 时钟控件
- android控件与常用控件
- Android 常用控件讲解 --控件
- Android常用控件
- Android常用控件-spinner
- Android常用控件-AutoCompleteTextView
- android常用控件(转)
- android 的常用控件
- Android常用控件总结
- Android常用控件总结
- Android控件常用属性
- Android常用控件总结
- Android常用控件总结
- Android控件常用属性
- Android常用控件总结
- android常用控件属性
- android 常用控件
- C语言的_Atomic类型限定符(C11)
- HDOJ 2115 I Love This Game
- Windows安装配置C/C++(VS2017)-OpenSSL(1.1.0f)开发环境
- Windows10资源管理器占用CPU过高解决办法
- 如何拿到tabbarcontroller,如何拿到主导航控制器
- Android常用控件
- Shell-/bin/bash和/bin/sh解释器的误用引起的脚本语法错误
- 埃拉托斯特尼筛法 学习心得
- CentOS中服务器时间设为自动同步的方法
- 利用Python实现基于协同过滤算法的影片推荐
- [安卓学习笔记]----意图
- C语言——第一部分 C语言概述以及编程基础
- 布局解决方案之居中布局
- Linux C编程学习笔记(2):open、creat、close函数及文件的创建、打开与关闭