zoukankan      html  css  js  c++  java
  • 5-3以复选框创建餐点选项

    .java

    public void takeOrder(View v){
        CheckBox chk;
        String msg="";
        int[] id={R.id.chk1,R.id.chk2,R.id.chk3,R.id.chk4,R.id.chk5,R.id.chk6};
        for (int i:id) {
            chk = (CheckBox) findViewById(i);
            if (chk.isChecked()) msg += "
    " + chk.getText();
        }
            if (msg.length()>0)  msg="你点购的餐点是:"+msg;
            else  msg="请点餐!";
    
            ( (TextView)findViewById(R.id.showOrder)).setText(msg);
    
    }
    View Code

    .xml
      1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      2     xmlns:tools="http://schemas.android.com/tools"
      3     android:layout_width="match_parent"
      4     android:layout_height="match_parent"
      5     android:paddingBottom="@dimen/activity_vertical_margin"
      6     android:paddingLeft="@dimen/activity_horizontal_margin"
      7     android:paddingRight="@dimen/activity_horizontal_margin"
      8     android:paddingTop="@dimen/activity_vertical_margin"
      9     tools:context="com.qh20161208.ch05_foodmenu.MainActivity"
     10     android:orientation="vertical">
     11 
     12 
     13 
     14     <LinearLayout
     15     android:layout_width="match_parent"
     16     android:layout_height="wrap_content"
     17     android:orientation="horizontal"
     18         android:layout_margin="5dp">
     19         <CheckBox
     20 
     21             android:layout_width="wrap_content"
     22             android:layout_height="wrap_content"
     23             android:text="汉堡"
     24             android:id="@+id/chk1"
     25             android:textSize="30sp" />
     26 
     27         <CheckBox
     28             android:layout_width="wrap_content"
     29             android:layout_height="wrap_content"
     30             android:text="薯条"
     31             android:id="@+id/chk2"
     32             android:textSize="30sp" />
     33 </LinearLayout>
     34 
     35     <LinearLayout
     36         android:layout_width="match_parent"
     37         android:layout_height="wrap_content"
     38         android:orientation="horizontal"
     39         android:layout_margin="5dp">
     40         <CheckBox
     41 
     42             android:layout_width="wrap_content"
     43             android:layout_height="wrap_content"
     44             android:text="可乐"
     45             android:id="@+id/chk3"
     46             android:textSize="30sp" />
     47 
     48         <CheckBox
     49             android:layout_width="wrap_content"
     50             android:layout_height="wrap_content"
     51             android:text="玉米浓汤"
     52             android:id="@+id/chk4"
     53             android:textSize="30sp" />
     54     </LinearLayout>
     55 
     56     <LinearLayout
     57         android:layout_width="match_parent"
     58         android:layout_height="wrap_content"
     59         android:orientation="horizontal"
     60         android:layout_margin="5dp">
     61         <CheckBox
     62 
     63             android:layout_width="wrap_content"
     64             android:layout_height="wrap_content"
     65             android:text="鸡块"
     66             android:id="@+id/chk5"
     67             android:textSize="30sp" />
     68 
     69         <CheckBox
     70             android:layout_width="wrap_content"
     71             android:layout_height="wrap_content"
     72             android:text="沙拉"
     73             android:id="@+id/chk6"
     74             android:textSize="30sp" />
     75     </LinearLayout>
     76 
     77     <LinearLayout
     78         android:orientation="horizontal"
     79         android:layout_width="match_parent"
     80         android:layout_height="70dp"
     81         android:layout_margin="5dp">
     82         <Button
     83             android:layout_width="match_parent"
     84             android:layout_height="wrap_content"
     85             android:text="订购"
     86             android:id="@+id/button"
     87             android:textSize="30sp"
     88             android:onClick="takeOrder"
     89             android:layout_gravity="center_horizontal" />
     90     </LinearLayout>
     91 
     92 
     93 
     94    
     95         <TextView
     96             android:id="@+id/showOrder"
     97             android:layout_width="match_parent"
     98             android:layout_height="wrap_content"
     99             android:text=""
    100             android:textSize="30sp"/>
    101 
    102    
    103 </LinearLayout>
    View Code

    isChecked()检查是否被选取,返回true或false。

    for循环逐一用数组中的资源ID调用findViewById()获取复选框对象,并调用isChecked()方法查看是否被选取。

    如果选取,用getText()方法获取其文字

    判断msg字段长度是否大于0,大于0表示循环添加了内容到字符串。

    获取文本框对象并显示。

  • 相关阅读:
    Django 查询filter 常见查询方法
    Django form表单
    Django的认证系统
    django 中间件
    tricks or tips
    Git
    Paper Reading: A Brief Introduction to Weakly Supervised Learning
    Paper Reading: Dynamic Routing Between Capsules (NIPS 2017)
    Paper Reading: Recombinator Networks: Learning Coarse-to-Fine Feature Aggregation (CVPR 2016)
    Paper Reading: 3D Hand Pose Estimation: From Current Achievementsto Future Goals (CVPR 2018)
  • 原文地址:https://www.cnblogs.com/huichao1314/p/6146326.html
Copyright © 2011-2022 走看看