Android编程学习之一:简单的控件使用
来源:互联网 发布:booking.it 编辑:程序博客网 时间:2024/06/01 19:40
这是本人第一次用CSDN发博客,而我发博客的目的是为了分享我学习编程的经历以及所遇到的问题困难,希望能得到大家的帮助和建议。
我所使用的Android Studio版本是2.2.3,用来调试的真机是CoolPad T1,Android版本为4.4.2。
下图是我写的一个小程序,在输入并选择对应的信息之后,点击“提交”按钮,将信息显示在屏幕下方。
界面设计
这个界面设计中用到了文本控件TextView、文本框控件EditText、按钮控件Button、单选框控件RadioButton以及多选框控件CheckBox五种控件,可以使用xml文件编写,例如(文本控件):
<TextView android:text="*1" android:layout_width="*2" android:layout_height="*3" android:id="*4"/>
*1为控件的文字内容,可以直接输入文字
*2,*3分别用于设置控件的宽和高,常用值有:fill_parent, wrap_content 以及具体的像素
*4是控件的id,格式为“@+id/你所设置的控件ID”
当然,还可以设置其他的属性,例如文字大小,文字颜色,背景颜色等。
具体的实例:
<TextView android:text="用户名称" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/admin" android:textSize="20sp" />
还有另外一种简单便捷的方式:直接拖拽控件到界面上,如1处,并且可以在2处设置属性值。
需要注意的是RadioButton控件,它需要和RadioGroup结合使用,保证每组只能选择一项
<RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:text="男" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioButton_man" android:layout_weight="1" android:textSize="20sp" /> <RadioButton android:text="女" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioButton_woman" android:layout_weight="1" android:textSize="20sp" /> </RadioGroup>
或者,可以直接将RadioButton拖拽进RadioGroup中
其他控件的使用和TextView类似,就不一一介绍了
具体功能实现
具体功能在java文件中实现,首先要定义各个控件并获取对象
private Button button1; private EditText editText1,editText2; private RadioButton radio1,radio2; private CheckBox check1,check2,check3,check4; private TextView text4;
获取对象使用的是findViewById()函数:
定义的对象名称 = (控件名) findViewById(R.id.控件ID);
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText1 = (EditText) findViewById(R.id.admin_edit); editText2 = (EditText) findViewById(R.id.edit_password); button1 = (Button) findViewById(R.id.button); radio1 = (RadioButton) findViewById(R.id.radioButton_man); radio2 = (RadioButton) findViewById(R.id.radioButton_woman); check1 = (CheckBox) findViewById(R.id.checkBox1); check2 = (CheckBox) findViewById(R.id.checkBox2); check3 = (CheckBox) findViewById(R.id.checkBox3); check4 = (CheckBox) findViewById(R.id.checkBox4); text4 = (TextView) findViewById(R.id.text_result);}
之后,就可以创建点击事件监听器OnClickListener,并与按钮对象关联
button1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ String result = "选择结果\n"; result += "用户名:" + editText1.getText() + "\n"; result += "密码:" + editText2.getText() + "\n"; result += "性别:"; if(radio1.isChecked()){ result += "男\n"; } else if (radio2.isChecked()){ result += "女\n"; } result += "爱好:"; if(check1.isChecked()){ result += "体育 "; } if(check2.isChecked()){ result += "音乐 "; } if(check3.isChecked()){ result += "阅读 "; } if(check4.isChecked()){ result += "上网 "; } text4.setText(result.trim()); } } );
这其中,还用到了getText()方法来获取EditText中的字符,isChecked()方法来判断按钮是否被选中,setText方法来设置TextView中的内容并显示
- Android编程学习之一:简单的控件使用
- Android学习笔记--6.自定义控件的简单使用
- android 编程实战 学习 之一
- Android的Service学习之一(简单的Service的框架)
- android 总结学习 简单的UI控件
- android学习之一·简单ListView的实现
- Android ListView 列表控件的简单使用
- Android ListView 列表控件的简单使用
- Android 控件系列: GridView 的简单使用
- android控件--Spinner的简单使用
- Android 控件:ViewFlipper的简单使用
- Android常用控件--ListView的简单使用
- Android常用控件--RecyclerView的简单使用
- Android 控件之ListView 的简单使用
- Android 开关控件Switch的简单使用
- Watir学习笔记之一 浏览器的简单使用
- Android 高级编程 RecyclerView 控件的使用
- Android之一个简单的NDK使用(一)
- [leetcode:python]9.Palindrom Number
- 借助数据库执行sql语句批量生成测试数据
- C++ HackerRank|Truck Tour
- ES安装X-pack后的默认账户与密码
- SSAS日期维度部署出错, 出现重复属性键
- Android编程学习之一:简单的控件使用
- 使用yum安装 MariaDB 数据库
- C语言总结(四)
- jquery的输入框自动补全功能+ajax
- Put Delete 请求报错Not Allow Method的解决方案
- spring activiti整合记录
- 源码阅读--xutil3
- CentOS NTP服务搭建
- java多线程学习(一)