- 버튼을 누르면 날짜와 시간 뜨게 하기 

 

<activity_main.xml>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="버튼을 클릭하면 오늘 날짜가 나옵니다." />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:text="오늘 날짜" />
</LinearLayout>

<MainActivity.java>

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    Button button;
    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = findViewById(R.id.editText);
        button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                String str = String.format("오늘은 %s", new Date());
//                editText.setText(str);
//                Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG).show();

                long now = System.currentTimeMillis();
                Date date = new Date(now);
                String patternKorea = "yyyy-MM-dd hh:mm:ss";
                SimpleDateFormat p1 = new SimpleDateFormat(patternKorea);
                String str = String.format(" %s", p1.format(date));
                editText.setText(str);
                Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG).show();
            }
        });
    }
}

 

-온도 변환 

 

<activity_main.xml>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="섭씨(ºC)"
        android:textSize="22sp"
        android:textStyle="bold"/>

    <EditText
        android:id="@+id/editCent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="36.5º" />

    <TextView
        android:id="@+id/txtFha"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="화씨(ºF)"
        android:textSize="22sp"
        android:textStyle="bold"/>

    <EditText
        android:id="@+id/editFha"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:text="0.0º" />


    <RadioGroup
        android:id="@+id/radio_Group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/radioFha"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="true"
            android:textSize="18sp"
            android:text="섭씨를 화씨(ºF)로" />

        <RadioButton
            android:id="@+id/radioCent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="화씨를 섭씨로(ºC)" />

    </RadioGroup>

    <Button
        android:id="@+id/btn_convert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="convert" />

    <Button
        android:id="@+id/btn_clear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="CLEAR" />
</LinearLayout>

 

<MainActivity.java>

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {
    EditText editCent, editFha;
    RadioButton radioFha, radioCent;
    Button btn_convert, btn_clear;

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

        editCent = findViewById(R.id.editCent);
        editFha = findViewById(R.id.editFha);
        radioFha = findViewById(R.id.radioFha);
        radioCent = findViewById(R.id.radioCent);
        btn_clear = findViewById(R.id.btn_clear);
        btn_convert = findViewById(R.id.btn_convert);

        btn_convert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (radioFha.isChecked()) {
                    double val = Double.parseDouble(editCent.getText().toString());
                    double change_value = 9.0/5.0*val + 32.0;
                    editFha.setText(String.format("%.2f", change_value));
                } else {
                    double val = Double.parseDouble(editFha.getText().toString());
                    double change_value = 5.0/9.0 *(val - 32);
                    editCent.setText(String.format("%.2f", change_value));
                }
            }
        });
        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editCent.setText("0.0");
                editFha.setText("0.0");
            }
        });
    }
}

+ Recent posts