예제: RadioButton,CheckBoxm,ImageView,Spinner

2019. 4. 8. 12:11Andriod

#1 java

package com.example.a_2;

 

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.Spinner;

import android.widget.TextView;

 

 

 

public class MainActivity extends AppCompatActivity {

EditText edt1,edt2;

RadioGroup group;

RadioButton g1,g2;

Spinner sp1;

CheckBox chk1,chk2,chk3;

TextView txt1,txt2;

ImageView img;

Button btn1;

String[] item;

String gender = "";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

 

edt1 = (EditText)findViewById(R.id.edt1);

edt2 = (EditText)findViewById(R.id.edt2);

group = (RadioGroup)findViewById(R.id.group);

chk1 = (CheckBox)findViewById(R.id.chk1);

chk2 = (CheckBox)findViewById(R.id.chk2);

chk3 = (CheckBox)findViewById(R.id.chk3);

txt1 = (TextView)findViewById(R.id.txt1);

txt2 = (TextView)findViewById(R.id.txt2);

img = (ImageView)findViewById(R.id.img);

sp1 = (Spinner)findViewById(R.id.sp1);

btn1 = (Button)findViewById(R.id.btn1);

g1 = (RadioButton)findViewById(R.id.g1);

g2 = (RadioButton)findViewById(R.id.g2);

 

item = new String[]{"선택하세요","A","B","O","AB"};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

android.R.layout.simple_spinner_item,item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

sp1.setAdapter(adapter);

 

btn1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

group.setOnCheckedChangeListener(mck);

int tall = Integer.parseInt(edt1.getText().toString());

int weight = Integer.parseInt(edt2.getText().toString());

double rat = weight/Math.pow((tall/100),2);

String str = Double.toString(rat);

txt1.setText("1. 혈액형은 "+sp1.getSelectedItem().toString()+"형 이고 " +gender+"입니다.");

txt2.setText("2. 신체질량지수는 "+ str+"입니다.");

if(chk1.isChecked()){

img.setImageResource(R.drawable.drinking);

}else if(chk2.isChecked()){

img.setImageResource(R.drawable.ciga);

}else if(chk3.isChecked()){

img.setImageResource(R.drawable.running);

}

}

});//btn1

}//onCreate

 

RadioGroup.OnCheckedChangeListener mck = new RadioGroup.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

if(group.getId()== R.id.group){

switch (checkedId){

case R.id.g1:

gender="남자";

break;

case R.id.g2:

gender="여자";

break;

}

}

}

};//RadioG

}

#2 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:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1">

 

<TextView

android:layout_width="50dp"

android:layout_height="50dp"

android:text="키: " />

 

<EditText

android:id="@+id/edt1"

android:layout_width="match_parent"

android:layout_height="50dp" />

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1">

<TextView

android:layout_width="50dp"

android:layout_height="50dp"

android:text="몸무게: "/>

<EditText

android:id="@+id/edt2"

android:layout_width="match_parent"

android:layout_height="50dp" />

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1">

<TextView

android:layout_width="50dp"

android:layout_height="50dp"

android:text="성별: " />

<RadioGroup

android:id="@+id/group"

android:layout_width="match_parent"

android:orientation="horizontal"

android:layout_height="50dp">

<RadioButton

android:id="@+id/g1"

android:layout_width="100dp"

android:layout_height="wrap_content"

android:text="남자"/>

<RadioButton

android:id="@+id/g2"

android:layout_width="100dp"

android:layout_height="50dp"

android:text="여자" />

</RadioGroup>

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1">

<TextView

android:layout_width="50dp"

android:layout_height="50dp"

android:text="혈액형: " />

<Spinner

android:id="@+id/sp1"

android:layout_width="wrap_content"

android:layout_height="50dp"

>

</Spinner>

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1">

<TextView

android:layout_width="70dp"

android:layout_height="50dp"

android:text="생활 습관 : " />

<CheckBox

android:id="@+id/chk1"

android:layout_width="60dp"

android:layout_height="50dp"

android:text="음주"/>

<CheckBox

android:id="@+id/chk2"

android:layout_width="60dp"

android:layout_height="50dp"

android:text="흡연"/>

<CheckBox

android:id="@+id/chk3"

android:layout_width="60dp"

android:layout_height="50dp"

android:text="운동"/>

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:orientation="vertical">

<Button

android:id="@+id/btn1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="몸상태 측정"/>

</LinearLayout>

 

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:orientation="vertical">

 

<TextView

android:id="@+id/txt1"

android:layout_width="wrap_content"

android:layout_height="30dp"

android:text="1." />

 

<TextView

android:id="@+id/txt2"

android:layout_width="wrap_content"

android:layout_height="30dp"

android:text="2." />

</LinearLayout>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:orientation="vertical">

<ImageView

android:id="@+id/img"

android:layout_width="wrap_content"

android:layout_height="fill_parent" />

</LinearLayout>

</LinearLayout>