物联网大赛 - Android学习笔记(二)Andriod 应用界面编程

学习目标:

  1. Android的程序界面和View组件

  2. View组件和ViewGroup组件

  3. 常见的布局管理器

  4. 文本框组件TextView和EditView

  5. 按钮组件Button 和ImageButton

  6. 特殊按钮RidoButton、CheckBox

  7. 事件显示组件AnalogClock与DigitalClock

  8.  使用AlertDialog创建弹窗

  9. Taost创建提示

  10. 使用Notification发送全局通知

 

一、概念:

Android应用开发中需要友好的图形界面,而android提供了丰富的UI组件,开发者需要使用这些单独的组件组成完美的UI界面

二、使用XML文件布局UI界面

 Android推荐使用XML布局视图,可以达到分离代码和视图原则。

在res/layout目录下定义任意一个xml文件,R.java会自动收录布局资源。设置activity显示视图。

setContentView(R.layout.<资源文件名称>);

注:当布局多个UI组件时,都可以为该UI组件指定android:id属性,在代码中访问

findViewById(R.id.<资源文件名称>);

在代码中获得UI组件之后:就可以通过代码控制UI组件的外观行为,包括组件的事件监听等等

实践:自定义一个UI界面

1、在Mainfest.xml文件中注册activity

<!--注册Activity-->
<activity android:name=".Infomation"></activity>

2、布局对应的UI视图文件

案例:设置背景图片轮播

//定义所有的轮播图片
  int[] images =new int[]{
          R.mipmap.img_01,R.mipmap.img_02,R.mipmap.img_03};

  //定义初始下标为0
  int CurrentIndex = 0;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_infomation);
     //获取ImageView
   final ImageView  iv =  (ImageView) findViewById(R.id.showView);
     iv.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {

            if(CurrentIndex >=2){

                CurrentIndex=-1;
            }
            //改变ImageView中的Src属性值
             iv.setImageResource(images[++CurrentIndex]);

         }
     });
  }

案例:通过单选按钮和复选框按钮获取用户信息

RadioButton  单选框、CheckBox复选框,单选框和复选框会多出指定android:checked 属性,该属性指定是否可以被选中。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:orientation="vertical">
    <android.support.v7.widget.AppCompatTextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center"
        android:text="选择相关信息" />
     <AutoCompleteTextView
         android:layout_width="wrap_content"
         android:id="@+id/showPersonInfo"
         android:layout_height="wrap_content" />
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rg">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/man"
            android:checked="true"
            android:text="男" />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/women"
            android:text="女" />
    </RadioGroup>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="红色" />
    <android.support.v7.widget.AppCompatCheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="蓝色"/>
</LinearLayout>

实践案例:计时器定时时钟

DigitalClock和AnalogClock都可以显示当前时间,不同的前者是数字时钟,可以显示当前秒数,后者是模拟时钟,不会显示当前秒数。Android中提供了Chronometer计时器

属性如下:基本属性如下

setBase()设置计时器开始时间

Start()开始计时

Stop()停止计时

btnStart.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //设置计时开始时间,获取系统时间
        ch.setBase(SystemClock.elapsedRealtime());
        //启动计时
        ch.start();
    }
});
//停止计时
btnEnd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        ch.stop();
    }
});

用户选择日期和时间DatePicker和TimePicker

用户选择日期,并为日期选择绑定监听器。

 

ListView列表视图

 ListView是常用的列表控件,以垂直列表的形式显示数据

常用属性

 


public class ListDate extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_date);
        //找到ListView控件
       ListView lv = (ListView)findViewById(R.id.List_it);
       //创建数组数据
        String[] arr = {"李白","杜甫","杜牧"};
        //将数组进行包装
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListDate.this,android.R.layout.simple_list_item_1,arr);
        lv.setAdapter(adapter);
        
    }
}

对话框使用:Toast

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_taost_test);
    Button btn =  (Button) findViewById(R.id.toastTest);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast.LENGTH_SHORT显示时间长短
            //show()调用显示方法
            Toast.makeText(TaostTest.this,"弹窗设置",Toast.LENGTH_SHORT).show();
        }
    });
}

猜你喜欢

转载自blog.csdn.net/weixin_44893902/article/details/108683632