zoukankan      html  css  js  c++  java
  • [2017-7-28]Android Learning Day6

     常用控件

    1. Spinner

    2. DatePickerDialog

    3. TimePickerDiaog

    4. RadioButton

    5. CheckBox

    spinner(下拉菜单)

    <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
     1 public class MainActivity extends AppCompatActivity {
     2 
     3     private Spinner s;
     4     private String[] dataSource = new String[]{"a1","b2","c3"};
     5 
     6     @Override
     7     protected void onCreate(Bundle savedInstanceState) {
     8         super.onCreate(savedInstanceState);
     9         setContentView(R.layout.activity_main);
    10 
    11         s = (Spinner) findViewById(R.id.spinner);
    12         //设置一个Adapter
    13         s.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataSource));
    14      //sOnSe                         OnIt
    15         s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    16             @Override
    17             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    18                 //在这里写监听事件
                System.out.println("用户选择的是"+dataSource[position]); 19 } 20 21 @Override 22 public void onNothingSelected(AdapterView<?> parent) { 23           //不写 24 } 25 }); 26 } 27 }

    DatePickerDialog(日期选择器)

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选择日期" />
     1 public class ChooseDate extends AppCompatActivity {
     2 
     3     @Override
     4     protected void onCreate(Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.activity_choose_date);
     7         slove();
     8     }
     9 
    10     public void slove() {
    11         findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
    12             @Override
    13             public void onClick(View v) {
    14                 new DatePickerDialog(ChooseDate.this, new DatePickerDialog.OnDateSetListener() {
    15                     @Override
    16                     public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    17                         String chooseDate = String.format("%04d-%02d-%02d",year,month+1,dayOfMonth);//"%04d"总长度4位,不够用0补齐
    18                         ((Button) findViewById(R.id.button)).setText(chooseDate);
    19                     }
    20                 },2000,0,1).show();//要注意月份是从0~11
    21             }
    22         });
    23     }
    24 }

    TimePickerDialog(时间选择器)

    这个就和日期选择器很像了

    <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="选择时间" />
     1 public class ChooseTime extends AppCompatActivity {
     2 
     3     @Override
     4     protected void onCreate(Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.activity_choose_date);
     7         slove();
     8     }
     9 
    10     public void slove() {
    11         findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
    12             @Override
    13             public void onClick(View v) {
    14                 new TimePickerDialog(ChooseTime.this, new TimePickerDialog.OnTimeSetListener() {
    15                     @Override
    16                     public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    17                         String chooseTime = String.format("%02d:%02d",hourOfDay,minute);
    18                         ((Button) findViewById(R.id.button)).setText(chooseTime);
    19                     }
    20                 },0,0,true).show();//True是否使用24小时制
    21             }
    22         });
    23     }
    24 }

    RadioButton(单选框)

    这个要注意的是,单选按钮不能单独存在,要放在RadioGroup里

     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:layout_height="match_parent"
     5     android:orientation="vertical">
     6     <LinearLayout
     7         android:layout_width="match_parent"
     8         android:layout_height="wrap_content"
     9         android:orientation="vertical"
    10         android:layout_weight="1">
    11 
    12         <TextView
    13             android:layout_width="match_parent"
    14             android:layout_height="wrap_content"
    15             android:layout_marginBottom="10dp"
    16             android:textSize="18sp"
    17             android:text="世界上最大的海洋是?"/>
    18 
    19         <RadioGroup
    20             android:layout_width="match_parent"
    21             android:layout_height="wrap_content" >
    22 
    23             <RadioButton
    24                 android:id="@+id/rbA"
    25                 android:layout_width="match_parent"
    26                 android:layout_height="wrap_content"
    27                 android:textSize="16sp"
    28                 android:text="A.太平洋" />
    29 
    30             <RadioButton
    31                 android:id="@+id/rbB"
    32                 android:layout_width="match_parent"
    33                 android:layout_height="wrap_content"
    34                 android:textSize="16sp"
    35                 android:text="B.北冰洋" />
    36         </RadioGroup>
    37 
    38     </LinearLayout>
    39 
    40     <Button
    41         android:id="@+id/chooseOne"
    42         android:layout_width="match_parent"
    43         android:layout_height="wrap_content"
    44         android:text="提交"/>
    45 
    46 </LinearLayout>
     1 public class ChooseSingle extends AppCompatActivity {
     2 
     3     @Override
     4     protected void onCreate(Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.activity_choose_single);
     7         slove();
     8     }
     9 
    10     private void slove() {
    11         findViewById(R.id.chooseOne).setOnClickListener(new View.OnClickListener() {
    12             @Override
    13             public void onClick(View v) {
    14                 RadioButton rb = (RadioButton) findViewById(R.id.rbA);
    15                 if(rb.isChecked()) {
    16                     Toast.makeText(ChooseSingle.this, "正确", Toast.LENGTH_SHORT).show();
    17                 }
    18                 else {
    19                     Toast.makeText(ChooseSingle.this, "错误", Toast.LENGTH_SHORT).show();
    20                 }
    21             }
    22         });
    23     }
    24 }

    CheckBox(多选框)

     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:layout_height="match_parent"
     5     android:orientation="vertical">
     6 
     7     <TextView
     8         android:id="@+id/tv1"
     9         android:layout_width="match_parent"
    10         android:layout_height="wrap_content"
    11         android:text="你喜欢喝什么饮料?" />
    12 
    13     <CheckBox
    14         android:id="@+id/cb1"
    15         android:layout_width="match_parent"
    16         android:layout_height="wrap_content"
    17         android:text="冰峰" />
    18 
    19     <CheckBox
    20         android:id="@+id/cb2"
    21         android:layout_width="match_parent"
    22         android:layout_height="wrap_content"
    23         android:text="果啤" />
    24 
    25     <CheckBox
    26         android:id="@+id/cb3"
    27         android:layout_width="match_parent"
    28         android:layout_height="wrap_content"
    29         android:text="雪碧" />
    30 
    31     <TextView
    32         android:id="@+id/tv2"
    33         android:layout_width="match_parent"
    34         android:layout_height="wrap_content" />
    35 </LinearLayout>
     1 package com.liwenchi.myapplication;
     2 
     3 import android.support.v7.app.AppCompatActivity;
     4 import android.os.Bundle;
     5 import android.view.View;
     6 import android.widget.CheckBox;
     7 import android.widget.CompoundButton;
     8 import android.widget.TextView;
     9 
    10 public class MainActivity extends AppCompatActivity {
    11 
    12     private CheckBox cb1,cb2,cb3;
    13     private TextView tv;
    14 
    15     @Override
    16     protected void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.activity_main);
    19         slove();
    20     }
    21 
    22     public void slove() {
    23         cb1 = (CheckBox) findViewById(R.id.cb1);
    24         cb2 = (CheckBox) findViewById(R.id.cb2);
    25         cb3 = (CheckBox) findViewById(R.id.cb3);
    26         tv = (TextView) findViewById(R.id.tv2);
    27         cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    28             @Override
    29             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    30                 onChanged();
    31             }
    32         });
    33         cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    34             @Override
    35             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    36                 onChanged();
    37             }
    38         });
    39         cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    40             @Override
    41             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    42                 onChanged();
    43             }
    44         });
    45     }
    46 
    47     public void onChanged() {
    48         String s = "我喜欢";
    49         if(cb1.isChecked()) {
    50             s += ","+cb1.getText();
    51         }
    52         if(cb2.isChecked()) {
    53             s += ","+cb2.getText();
    54         }
    55         if(cb3.isChecked()) {
    56             s += ","+cb3.getText();
    57         }
    58         tv.setText(s);
    59     }
    60 }
  • 相关阅读:
    CF1416D Graph and Queries
    Wordpress建站系统相关
    微观经济学
    Preface
    Thread pool in chromium
    [fllutter engine] 并发消息队列
    bugku misc
    python 3.1学习
    HTML&CSS
    DOM技术点
  • 原文地址:https://www.cnblogs.com/liwenchi/p/7247469.html
Copyright © 2011-2022 走看看