经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android SDK » 查看文章
Android Studio教程06-布局,监听器以及基本控件
来源:cnblogs  作者:Bricker666  时间:2019/1/18 9:24:56  对本文有异议

目录

2. 监听器

  • 一个控件可以设置多个监听器
  • 绑定监听器的步骤
    • 获取代表控件的对象
    • 定义一个类,实现监听器接口
    • 生成监听器对象
    • 为控件绑定监听器对象
  1. public class MainActivity extends AppCompatActivity {
  2. private Button bt;
  3. private TextView tv;
  4. int count=0;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. bt = (Button)findViewById(R.id.bt1);
  10. tv = (TextView)findViewById(R.id.hello);
  11. //生成监听器对象 new ButtonListener()
  12. //为控件绑定监听器对象 bt.setOnClickListener
  13. bt.setOnClickListener(new ButtonListener());
  14. System.out.println("--MainActivity: OnCreate--");
  15. }
  16. // 定义一个类,实现监听器接口
  17. class ButtonListener implements View.OnClickListener{
  18. @Override
  19. public void onClick(View v) {
  20. count++;
  21. tv.setText(count+"");
  22. }
  23. }
  24. }

3. 布局

  • 控件布局方法:就是控制控件在Activity中的位置,大小,颜色以及其他控件样式属性的方法
  • 如何设置布局
    • 在布局文件完成控件布局
    • 在Java代码中完成控件布局

3.1. 布局分类

(1). Linear Layout

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:orientation="vertical"
  5. android:layout_height="match_parent">
  6. <TextView
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:background="#FF0000"
  10. android:text="First text view"/>
  11. <TextView
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:background="#00FF00"
  15. android:text=" Second Text View"/>
  16. </LinearLayout>

(2). Relative Layout

(3). ListView

(4). Grid View

4. 其他比较杂的内容

4.1. 距离单位的区别px,dp,sp

  • px: 像素分辨率,屏幕是480*800个像素,每个像素可以显示一个RGB颜色

  • dpi:屏幕细腻程度

  • dp:设备无关像素(最主要)

为什么使用dp?

  • sp: 可以缩放的像素:用于指定字体大小

4.2. 控件的外边距和内边距

1. 什么是内外边距

2. 如何设置内外边距

5. Android控件

5.1.多选按钮CheckBox

1. 如何使用CheckBox

  1. private CheckBox eatbox, sleppbox, dotabox;
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.frist_layout);
  6. eatbox = (CheckBox)findViewById(R.id.eatId);
  7. sleppbox = (CheckBox)findViewById(R.id.sleppId);
  8. dotabox = (CheckBox)findViewById(R.id.dotaId);
  9. onBoxClickListener listener = new onBoxClickListener();
  10. eatbox.setOnClickListener(listener);
  11. sleppbox.setOnClickListener(listener);
  12. dotabox.setOnClickListener(listener);
  13. }

配置文件如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:orientation="vertical"
  5. android:layout_height="match_parent">
  6. <CheckBox
  7. android:id="@+id/eatId"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="吃饭"/>
  11. <CheckBox
  12. android:id="@+id/sleppId"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="睡觉"/>
  16. <CheckBox
  17. android:id="@+id/dotaId"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="data"/>
  21. </LinearLayout>

2. 常用onClickListeneronCheckedChangeListener监听器

2.1. onClickListener监听器

  1. class onBoxClickListener implements View.OnClickListener{
  2. // view参数是调用setOnClickListener的对象
  3. // view是checkbox的父类
  4. // view.getId--查看是哪个对象调用的这个方法
  5. @Override
  6. public void onClick(View v) {
  7. // 向下转型
  8. CheckBox box = (CheckBox)v;
  9. if (v.getId() == R.id.eatId){
  10. System.out.println("eat is clicked");
  11. }
  12. else if (v.getId() == R.id.sleppId){
  13. System.out.println("slepp is clicked");
  14. }
  15. else if (v.getId() ==R.id.dotaId){
  16. System.out.println("dota is clicked");
  17. }
  18. // checkbox 是否选中
  19. if (box.isChecked()){
  20. System.out.println("clicked");
  21. }
  22. else{
  23. System.out.println("Not clicked");
  24. }
  25. }
  26. }

2.2. onCheckedChangeListener监听器

CompoundButton

  1. // 选中的时候就会调用这个状态
  2. class CheckBoxListener implements CompoundButton.OnCheckedChangeListener{
  3. @Override
  4. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  5. if (buttonView.getId() == R.id.eatId){
  6. System.out.println("eat is clicked");
  7. }
  8. else if (buttonView.getId() == R.id.sleppId){
  9. System.out.println("slepp is clicked");
  10. }
  11. else if (buttonView.getId() ==R.id.dotaId){
  12. System.out.println("dota is clicked");
  13. }
  14. // checkbox 是否选中
  15. if (isChecked){
  16. System.out.println("clicked");
  17. }
  18. else{
  19. System.out.println("Not clicked");
  20. }
  21. }
  22. }

原文链接:http://www.cnblogs.com/haochen273/p/10284691.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号