目前分類:Android (5)

瀏覽方式: 標題列表 簡短摘要

res/layou/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.lai.class5_spinnertospinner.MainActivity">


    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:entries="@array/Citys"
        android:id="@+id/spinner" />

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:id="@+id/spinner2" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:textSize="30sp"
        android:layout_weight="6"
        android:id="@+id/textView" />
</LinearLayout>

res/values/strings.xml

<resources>
    <string name="app_name">Class5_SpinnerToSpinner</string>
    <string-array name="Citys">
        <item>台北市</item>
        <item>台中市</item>
        <item>高雄市</item>
    </string-array>
    <string-array name="SubCity1">
        <item>信義區</item>
        <item>大安區</item>
        <item>內湖區</item>
    </string-array>
    <string-array name="SubCity2">
        <item>東區</item>
        <item>中區</item>
        <item>南屯區</item>
    </string-array>
    <string-array name="SubCity3">
        <item>鳳山區</item>
        <item>三民區</item>
        <item>旗津區</item>
    </string-array>


</resources>

java/MainActivity

package com.lai.class5_spinnertospinner;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Spinner sp1,sp2;
    TextView tvShow;
    String strCity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findviews();
//        testSpinner();
    }

    void findviews(){
        sp1 = (Spinner)findViewById(R.id.spinner);
        sp1.setOnItemSelectedListener(spinnerListener);
        sp2 = (Spinner)findViewById(R.id.spinner2);
        //註冊同一個監聽器spinnerListener
        sp2.setOnItemSelectedListener(spinnerListener);

        tvShow = (TextView)findViewById(R.id.textView);
    }

    //等同於xml裡的android:entries="@array/Citys"
    void testSpinner(){
        String[] data={"a","b","c"};

        ArrayAdapter<String> adt=new ArrayAdapter<String>(
                this,   //畫面Content
                android.R.layout.simple_spinner_item,   //用android內建的資源檔
                data);  //資料
        //設定下拉式樣式
        adt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //
        sp2.setAdapter(adt); //註冊

    }

    AdapterView.OnItemSelectedListener spinnerListener=new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long id) {
            //第一種取文字方法
//            String text=((TextView)view).getText().toString();
            //第二種取文字方法
            //String text=adapterView.getItemAtPosition(i).toString();
//            Toast.makeText(MainActivity.this,
//                            text,
//                            Toast.LENGTH_SHORT).show();
                if(adapterView.getId()==sp1.getId()){
                    //大城市
                     strCity=((TextView)view).getText().toString();
                    //小地方
                    String[] subcity=null;

                    if(i==0){
                        subcity=getResources().getStringArray(R.array.SubCity1);


                    }
                    if(i==1){
                        subcity=getResources().getStringArray(R.array.SubCity2);

                    }
                    if(i==2){
                        subcity=getResources().getStringArray(R.array.SubCity3);
                    }
                    ArrayAdapter<String> adt=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,subcity);
                    adt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    sp2.setAdapter(adt);
                }else{

                    String strSubCity=((TextView)view).getText().toString();
                    //將spinner裡的內容顯示在textview裡
                    tvShow.setText(strCity+"-"+strSubCity);


                }


        }



        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };
}

 

mingyilai 發表在 痞客邦 留言(0) 人氣()

res/layou/activity_main.xml

package com.lai.class4_changecolor;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    Spinner spinner;
    RadioButton rbRed,rbGreen,rbBlue;
    SeekBar seekBar;
    ToggleButton tgb;
    TextView tvSelect,tvShow;


    int fRed,fGreen,fBlue;  //定義前景色
    int bRed,bGreen,bBlue;  //定義背景色
    int pos;  //選到的位置

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findviews();
    }

    void findviews(){
        spinner = (Spinner)findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(spinnerListener);

        //一個監聽器監聽多個元件
        rbRed = (RadioButton)findViewById(R.id.radioButton);
        rbRed.setOnClickListener(rbsClick);
        rbGreen = (RadioButton)findViewById(R.id.radioButton2);
        rbGreen.setOnClickListener(rbsClick);
        rbBlue = (RadioButton)findViewById(R.id.radioButton3);
        rbBlue.setOnClickListener(rbsClick);

        seekBar = (SeekBar)findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(seekBarChangeListener);

        tgb = (ToggleButton)findViewById(R.id.toggleButton);
        tgb.setOnCheckedChangeListener(tgbListener);

        tvSelect = (TextView)findViewById(R.id.textView);
        tvShow = (TextView)findViewById(R.id.textView2);
    }

    //一個監聽器監聽多個元件
    View.OnClickListener rbsClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                //紅色區塊
                case R.id.radioButton:
                    if(pos==0) {
                        seekBar.setProgress(fRed); //設定並記憶前景色的位置
                    }else{
                        seekBar.setProgress(bRed);
                    }
                    tvSelect.setText(rbRed.getText());
//                    tvSelect.setTextColor(0xFFFF0000); //一定要八碼顏色
                    tvSelect.setTextColor(Color.RED);
                    break;
                //綠色區塊
                case R.id.radioButton2:
                    if(pos==0) {
                        seekBar.setProgress(fGreen);//設定並記憶前景色的位置
                    }else{
                        seekBar.setProgress(bGreen);
                    }
                    tvSelect.setText(rbGreen.getText());
                    tvSelect.setTextColor(Color.GREEN);
                    break;
                //藍色區塊
                case R.id.radioButton3:
                    if(pos==0) {
                        seekBar.setProgress(fBlue);//設定並記憶前景色的位置
                    }else{
                        seekBar.setProgress(bBlue);
                    }
                    tvSelect.setText(rbBlue.getText());
                    tvSelect.setTextColor(Color.BLUE);
                    break;
            }

        }
    };

    //seekBar監聽器
    SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if(pos == 0) {
                //如果顏色被選取的話,下面文字顏色會隨SEEKBAR的拖拉而改
                if (rbRed.isChecked()) {
//                tvShow.setTextColor(Color.rgb(progress,0,0));//只改變文字顏色
                    fRed = progress;    //前景色=progress(跟著改變)
                }
                if (rbGreen.isChecked()) {
//                tvShow.setTextColor(Color.rgb(0,progress,0));//只改變文字顏色
                    fGreen = progress;  //前景色=progress(跟著改變)
                }
                if (rbBlue.isChecked()) {
//                tvShow.setTextColor(Color.rgb(0,0,progress));//只改變文字顏色
                    fBlue = progress;   //前景色=progress(跟著改變)
                }
                //設定文字顏色
                tvShow.setTextColor(Color.rgb(fRed, fGreen, fBlue));//縮減程式碼剩這行,功能一樣

            }else{
                if (rbRed.isChecked()) {
//                tvShow.setTextColor(Color.rgb(progress,0,0));//只改變文字顏色
                    bRed = progress;    //背景色=progress(跟著改變)
                }
                if (rbGreen.isChecked()) {
//                tvShow.setTextColor(Color.rgb(0,progress,0));//只改變文字顏色
                    bGreen = progress;  //背景色=progress(跟著改變)
                }
                if (rbBlue.isChecked()) {
//                tvShow.setTextColor(Color.rgb(0,0,progress));//只改變文字顏色
                    bBlue = progress;   //背景色=progress(跟著改變)
                }
                //設定背景顏色
                tvShow.setBackgroundColor(Color.rgb(fRed, fGreen, fBlue));

            }


        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {


        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {


        }
    };

    //spinner監聽器
    AdapterView.OnItemSelectedListener spinnerListener=new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                    //(容器,每一個元件都是VIEW且用TEXTVIEW裝,從0開始
            //前景色是0,背景色是1
            pos=position; //監聽器

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };

    //toggleButton監聽器
    CompoundButton.OnCheckedChangeListener tgbListener=new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                tvShow.setBackgroundColor(Color.WHITE);
            }else{
                tvShow.setBackgroundColor(Color.BLACK);
            }

        }
    };
}

java/MainActivity

package com.lai.class4_changecolor;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    Spinner spinner;
    RadioButton rbRed,rbGreen,rbBlue;
    SeekBar seekBar;
    ToggleButton tgb;
    TextView tvSelect,tvShow;


    int fRed,fGreen,fBlue;  //定義前景色
    int bRed,bGreen,bBlue;  //定義背景色
    int pos;  //選到的位置

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findviews();
    }

    void findviews(){
        spinner = (Spinner)findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(spinnerListener);

        //一個監聽器監聽多個元件
        rbRed = (RadioButton)findViewById(R.id.radioButton);
        rbRed.setOnClickListener(rbsClick);
        rbGreen = (RadioButton)findViewById(R.id.radioButton2);
        rbGreen.setOnClickListener(rbsClick);
        rbBlue = (RadioButton)findViewById(R.id.radioButton3);
        rbBlue.setOnClickListener(rbsClick);

        seekBar = (SeekBar)findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(seekBarChangeListener);

        tgb = (ToggleButton)findViewById(R.id.toggleButton);
        tgb.setOnCheckedChangeListener(tgbListener);

        tvSelect = (TextView)findViewById(R.id.textView);
        tvShow = (TextView)findViewById(R.id.textView2);
    }

    //一個監聽器監聽多個元件
    View.OnClickListener rbsClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                //紅色區塊
                case R.id.radioButton:
                    if(pos==0) {
                        seekBar.setProgress(fRed); //設定並記憶前景色的位置
                    }else{
                        seekBar.setProgress(bRed);
                    }
                    tvSelect.setText(rbRed.getText());
//                    tvSelect.setTextColor(0xFFFF0000); //一定要八碼顏色
                    tvSelect.setTextColor(Color.RED);
                    break;
                //綠色區塊
                case R.id.radioButton2:
                    if(pos==0) {
                        seekBar.setProgress(fGreen);//設定並記憶前景色的位置
                    }else{
                        seekBar.setProgress(bGreen);
                    }
                    tvSelect.setText(rbGreen.getText());
                    tvSelect.setTextColor(Color.GREEN);
                    break;
                //藍色區塊
                case R.id.radioButton3:
                    if(pos==0) {
                        seekBar.setProgress(fBlue);//設定並記憶前景色的位置
                    }else{
                        seekBar.setProgress(bBlue);
                    }
                    tvSelect.setText(rbBlue.getText());
                    tvSelect.setTextColor(Color.BLUE);
                    break;
            }

        }
    };

    //seekBar監聽器
    SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if(pos == 0) {
                //如果顏色被選取的話,下面文字顏色會隨SEEKBAR的拖拉而改
                if (rbRed.isChecked()) {
//                tvShow.setTextColor(Color.rgb(progress,0,0));//只改變文字顏色
                    fRed = progress;    //前景色=progress(跟著改變)
                }
                if (rbGreen.isChecked()) {
//                tvShow.setTextColor(Color.rgb(0,progress,0));//只改變文字顏色
                    fGreen = progress;  //前景色=progress(跟著改變)
                }
                if (rbBlue.isChecked()) {
//                tvShow.setTextColor(Color.rgb(0,0,progress));//只改變文字顏色
                    fBlue = progress;   //前景色=progress(跟著改變)
                }
                //設定文字顏色
                tvShow.setTextColor(Color.rgb(fRed, fGreen, fBlue));//縮減程式碼剩這行,功能一樣

            }else{
                if (rbRed.isChecked()) {
//                tvShow.setTextColor(Color.rgb(progress,0,0));//只改變文字顏色
                    bRed = progress;    //背景色=progress(跟著改變)
                }
                if (rbGreen.isChecked()) {
//                tvShow.setTextColor(Color.rgb(0,progress,0));//只改變文字顏色
                    bGreen = progress;  //背景色=progress(跟著改變)
                }
                if (rbBlue.isChecked()) {
//                tvShow.setTextColor(Color.rgb(0,0,progress));//只改變文字顏色
                    bBlue = progress;   //背景色=progress(跟著改變)
                }
                //設定背景顏色
                tvShow.setBackgroundColor(Color.rgb(fRed, fGreen, fBlue));

            }


        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {


        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {


        }
    };

    //spinner監聽器
    AdapterView.OnItemSelectedListener spinnerListener=new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                    //(容器,每一個元件都是VIEW且用TEXTVIEW裝,從0開始
            //前景色是0,背景色是1
            pos=position; //監聽器

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };

    //toggleButton監聽器
    CompoundButton.OnCheckedChangeListener tgbListener=new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                tvShow.setBackgroundColor(Color.WHITE);
            }else{
                tvShow.setBackgroundColor(Color.BLACK);
            }

        }
    };
}

 

文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()

res/layou/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.lai.class_bmi.MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:textSize="20sp"
            android:text="@string/height"
            />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:textSize="20sp"
            android:id="@+id/editText" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="@string/weight"
            android:layout_margin="10dp"
             />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:textSize="20sp"
            android:id="@+id/editText2" />
    </LinearLayout>
    <!--android:onClick="onCaculate"是第五種方法-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/submit"
        android:layout_gravity="center"
        android:onClick="onCaculate"
        android:id="@+id/button" />
</LinearLayout>

res/layou/activity_bmi.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/height"
            android:textSize="20sp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text=" "
            android:textSize="20sp"
            android:id="@+id/textView4" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/weight"
            android:textSize="20sp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:textSize="20sp"
            android:text=" "
            android:id="@+id/textView6" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_margin="10dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/BMI"
            android:textSize="20sp"
             />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text=" "
            android:textSize="20sp"
            android:id="@+id/textView8" />
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="onBack"
        android:text="回上一頁"
        android:id="@+id/button2" />
</LinearLayout>

java/MainActivity

package com.lai.class_bmi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    /* 先做第一個畫面的元件宣告 */
    EditText etHeight,etWeight;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* 載入畫面 */
        setContentView(R.layout.activity_main);

        findviews();
    }

    void findviews(){
        /* 第一個畫面 */
        etHeight=(EditText)findViewById(R.id.editText);
        etWeight=(EditText)findViewById(R.id.editText2);

    }
//    View.OnClickListener btnclick=new View.OnClickListener() {
//        @Override
//        public void onClick(View v) {
//
//
//        }
//    };

                /*View一定要放 */
    public void onCaculate(View v){
        Intent intent=new Intent();
        /* 轉換畫面 intent.setClass(現在的畫面,要切換的畫面);  */
        intent.setClass(MainActivity.this,BmiActivity.class);
        /* 帶資料  */
        Bundle b=new Bundle();
        /* b.putString(名稱 , 要帶的值) */
        b.putString("h",etHeight.getText().toString());
        b.putString("w",etWeight.getText().toString());
        /* 將要帶的資料 b 用putExtras帶過去 */
        intent.putExtras(b);
        /* 啟用畫面 */
        startActivity(intent);



    }

    /* 隱含的方法  */
    void implicitActivity(){
//        Intent intent=new Intent();
//        intent.setAction("com.lai.bmi");
//        startActivity(intent);
    }
}

java/BmiActivity

package com.lai.class_bmi;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import java.text.NumberFormat;

/**
 * Created by mingyilai on 2016/8/12.
 */

            /* 要先extends畫面 */
public class BmiActivity extends AppCompatActivity {
    Bundle b;//要帶的資料
    TextView tvHeight,tvWeight,tvBmi;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* 載入BMI的畫面 */
        setContentView(R.layout.activity_bmi);

        /* 建立接收元件intent */
        Intent intent=getIntent();
        if(intent == null){
            return;
        }

        /* 接收資料 */
        b=intent.getExtras();
        /* 接收值 */
        b.getString("h","w");

        findviews();
        setdata(); //設定值
    }

    void findviews(){
        tvHeight = (TextView)findViewById(R.id.textView4);
        tvWeight = (TextView)findViewById(R.id.textView6);
        tvBmi = (TextView)findViewById(R.id.textView8);
    }

    void setdata(){
        tvHeight.setText(b.getString("h"));
        tvWeight.setText(b.getString("w"));
        //將接收的值轉換成數字
        double h=Double.parseDouble(b.getString("h"))/100;
        double w=Double.parseDouble(b.getString("w"));
        double bmi=w/(h*h);
        //先叫出NumberFormat的功能
        NumberFormat nf=NumberFormat.getInstance();
        //只取小數點後面2位
        nf.setMaximumFractionDigits(2);

        /* textview用 append串接字串 */
        tvBmi.setText(String.valueOf(nf.format(bmi)) + "\n");
        if(bmi >= 35) {
            tvBmi.append("重度肥胖");
        }else if(bmi < 35 && bmi >= 30){
            tvBmi.append("中度肥胖");
        }else if(bmi < 30 && bmi >= 27){
            tvBmi.append("輕度肥胖");
        }else if(bmi < 27 && bmi >= 24){
            tvBmi.append("過重");
        }else if(bmi < 24 && bmi >= 18.5){
            tvBmi.append("正常範圍");
        }else{
            tvBmi.append("體重過輕");
        }
    }



    public void onBack(View v){
//        /* Back stack */
//        Intent back=new Intent(BmiActivity.this,MainActivity.class);
//        /* 開新畫面 */
//        startActivity(back);

        /* 沒有Back stack */
        BmiActivity.this.finish();
    }
}

 

 

文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()

res/layou/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/pic"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.lai.class3_order.MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tableNumber"
            android:textSize="@dimen/BigText"
            android:textColor="@color/colorAccent"
            android:id="@+id/textView" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText" />
    </LinearLayout>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_margin="20dp"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/BigText"
            android:text="@string/normal"
            android:textColor="@color/colorAccent"
            android:id="@+id/radioButton" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/BigText"
            android:textColor="@color/colorAccent"
            android:text="@string/vip"
            android:id="@+id/radioButton2" />
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/meal"

        android:textSize="@dimen/BigText"
        android:layout_gravity="center_horizontal"
        android:id="@+id/textView2" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:gravity="center_horizontal"
        android:layout_margin="20dp"
        android:layout_height="wrap_content">

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/BigText"
            android:text="@string/ameal"
            android:id="@+id/checkBox" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:text="@string/bmeal"
            android:textSize="@dimen/BigText"
            android:id="@+id/checkBox2"
            android:checked="false" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:gravity="center_horizontal"
        android:layout_margin="20dp"
        android:layout_height="wrap_content">

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/BigText"
            android:text="@string/cmeal"
            android:id="@+id/checkBox3" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/BigText"
            android:text="@string/dmeal"
            android:id="@+id/checkBox4" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textColor="@color/colorAccent"
        android:textSize="@dimen/BigText"
        android:text="@string/beverage"
        android:id="@+id/textView3" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/BigText"
            android:text="@string/tea"
            android:id="@+id/radioButton3" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/BigText"
            android:text="@string/coffee"
            android:id="@+id/radioButton4" />
    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="20dp"
        android:textColor="@color/colorAccent"
        android:textSize="@dimen/BigText"
        android:text="@string/submit"
        android:id="@+id/button" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textColor="@color/colorAccent"
        android:textSize="@dimen/BigText"
        android:text="@string/totalMoney"
        android:padding="20dp"
        android:id="@+id/textView4" />
</LinearLayout>

 

java/MainActivity

package com.lai.class3_order;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText etTableNumber;
    RadioButton rbNormal,rbVip,rbTea,rbCoffee;
    CheckBox cbA,cbB,cbC,cbD;
    Button btn;
    TextView tvShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findviews();
    }

    void findviews(){
        etTableNumber = (EditText)findViewById(R.id.editText);
        rbNormal = (RadioButton)findViewById(R.id.radioButton);
        rbVip = (RadioButton)findViewById(R.id.radioButton2);
        rbTea = (RadioButton)findViewById(R.id.radioButton3);
        rbCoffee = (RadioButton)findViewById(R.id.radioButton4);
        cbA = (CheckBox)findViewById(R.id.checkBox);
        cbA.setTag(100.0); //新增價錢的標籤
        cbB = (CheckBox)findViewById(R.id.checkBox2);
        cbB.setTag(150.0);
        cbC = (CheckBox)findViewById(R.id.checkBox3);
        cbC.setTag(200.0);
        cbD = (CheckBox)findViewById(R.id.checkBox4);
        cbD.setTag(250.0);
        btn = (Button)findViewById(R.id.button);
        tvShow = (TextView)findViewById(R.id.textView4);
        //設定監聽器
        btn.setOnClickListener(btnListener);

    }

    //建立監聽器
    View.OnClickListener btnListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double total=0.0;
            StringBuilder sb = new StringBuilder();
            sb.append("桌號").append(etTableNumber.getText().toString()).append("\n");
            if(rbNormal.isChecked()){
                sb.append(rbNormal.getText()).append("\n");
            }else{
                sb.append(rbVip.getText()).append("\n");
            }
            //主餐的目錄
            sb.append(getResources().getString(R.string.meal)).append("\n").append(" ");
            if(cbA.isChecked()){
                sb.append(cbA.getText()).append(" ");
                total+=(double)cbA.getTag();
            }
            if(cbB.isChecked()){
                sb.append(cbB.getText()).append(" ");
                total+=(double)cbB.getTag();
            }
            if(cbC.isChecked()){
                sb.append(cbC.getText()).append(" ");
                total+=(double)cbC.getTag();
            }
            if(cbD.isChecked()){
                sb.append(cbD.getText()).append(" ");
                total+=(double)cbD.getTag();
            }
            //附餐的目錄
            sb.append("\n").append(getResources().getString(R.string.beverage)).append("\n").append(" ");
            if(rbTea.isChecked()){
                sb.append(rbTea.getText()).append("\n");
            }else{
                sb.append(rbCoffee.getText()).append("\n");
            }

            if(rbVip.isChecked()){
                sb.append(getResources().getString(R.string.totalMoney)).append(total*0.9).append(getResources()).append(getString(R.string.dollar));
            }else{
                sb.append(getResources().getString(R.string.totalMoney)).append(total).append(getResources()).append(getString(R.string.dollar));

            }

            tvShow.setText(sb);

        }
    };
}
文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()

package net.macdidi.class1_exchange;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText etInput;
    TextView tvShow;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findviews();
    }
    void findviews(){
        etInput=(EditText)findViewById(R.id.editText);
        tvShow=(TextView)findViewById(R.id.textView3);
        //1.建立元件
        btn=(Button)findViewById(R.id.button);
        //3.註冊監聽器
        btn.setOnClickListener(btnListener);

        //第三種
//        btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });

        //第四種
//        btn.setOnClickListener(this);



    }

    //2.建立監聽器(匿名內部類)最常用的一種
    View.OnClickListener btnListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double usd=0;
            String strUsd=etInput.getText().toString();
            try {
                usd = Double.parseDouble(strUsd);
            }catch(NumberFormatException e){
                e.printStackTrace();
                Toast.makeText(MainActivity.this,"輸入數字格式不正確", Toast.LENGTH_SHORT).show();
                //(this指所在類別的物件,要顯示的文字,出現時間)
            }
            double twd=usd*30.0;
            tvShow.setText(String.valueOf(twd));
        }
    };

    @Override
    public void onClick(View v) {

    }

    //配合第四種
//    @Override
//    public void onClick(View v) {
//
//    }


    //第二種
//    class BtnClick implements View.OnClickListener{
//
//        @Override
//        public void onClick(View v) {
//
//        }
//    };
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="net.macdidi.class1_exchange.MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="160dp"
            android:layout_height="wrap_content"
            android:text="@string/USD"
            android:textColor="@color/blue"
            android:textSize="@dimen/BigText"
            android:id="@+id/textView" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/input"
            android:id="@+id/editText" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/TWD"
            android:textColor="#aa3700ff"
            android:textSize="@dimen/BigText"
            android:id="@+id/textView2" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" "
            android:id="@+id/textView3" />
    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/exchange"
        android:textSize="30sp"
        android:id="@+id/button" />

</LinearLayout>

 

文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()