글 번호: 138 작성자: gihun 작성시간: 2023-11-01 09:57:41.326 조회수: 221

백업



package com.example.myapplication2;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TabHost;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTabHost;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private FragmentTabHost tabHost;
    private double bmiResult;
    

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

        setTitle("학교 모바일 발표");
        tabHost = findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        // 입력 탭 추가        tabHost.addTab(tabHost.newTabSpec("Input").setIndicator("입력"), InputFragment.class, null);
        // 결과 탭 추가        tabHost.addTab(tabHost.newTabSpec("Result").setIndicator("결과"), ResultFragment.class, null);

        // 탭 변경 이벤트 처리        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override            public void onTabChanged(String tabId) {  // 탭이 변경될 때마다 호출                Log.d("MainActivity", "onTabChanged: " + tabId);
                if (tabId.equals("Result")) {
                    ResultFragment resultFragment = (ResultFragment) getSupportFragmentManager().findFragmentByTag("Result");
                    resultFragment.updateBMI(bmiResult);
                }
            }
        });
    }

    double calcBMI(double weight, double heightInCm) {
        double heightInMeters = heightInCm / 100; // cm를 m로 변환        return weight / (heightInMeters * heightInMeters);
    }

    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem resetItem = menu.add(0, 1, 0, "Reset");
        return true;
    }

    @Override    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                InputFragment inputFragment = (InputFragment) getSupportFragmentManager().findFragmentByTag("Input");
                inputFragment.resetInputs();
                tabHost.setCurrentTabByTag("Input");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}



<?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"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">


    <androidx.fragment.app.FragmentTabHost
            android:id="@android:id/tabhost"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            <!-- TabWidget for displaying tabs -->
            <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            <!-- FrameLayout for displaying the content of the selected tab -->
            <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" />

        </LinearLayout>

    </androidx.fragment.app.FragmentTabHost>
</LinearLayout>


package com.example.myapplication2;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;

import java.util.Locale;


public class ResultFragment extends Fragment {
    TextView tvResult;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_result, container, false);
        tvResult = view.findViewById(R.id.tvResult);
        return view;
    }

    public void updateBMI(double bmi) {
        if (tvResult != null) {
            Log.d("ResultFragment", String.format(Locale.getDefault(), "updateBMI: %.2f", bmi));
            tvResult.setText(String.format("BMI 결과: %.2f", bmi));
        }
    }
}

package com.example.myapplication2;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import androidx.fragment.app.Fragment;

public class InputFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_input, container, false);
    }



    public void resetInputs() {
        View view = getView();  // getView()의 반환값을 로컬 변수에 저장
        if (view != null) {  // view가 null이 아닌지 확인
            EditText etName = view.findViewById(R.id.etName);
            EditText etHeight = view.findViewById(R.id.etHeight);
            EditText etWeight = view.findViewById(R.id.etWeight);

            etName.setText("");
            etHeight.setText("");
            etWeight.setText("");
        }
    }


}



<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabInput"
        android:orientation="vertical">

    <EditText
            android:id="@+id/etName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="이름"/>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:weightSum="1">

        <EditText
                android:id="@+id/etHeight"
                android:layout_width="0dp"
                android:layout_weight="0.9"
                android:layout_height="wrap_content"
                android:hint="키"/>

        <TextView
                android:layout_width="0dp"
                android:layout_weight="0.1"
                android:layout_height="wrap_content"
                android:text="cm"
                android:gravity="center"/>

    </LinearLayout>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:weightSum="1">

        <EditText
                android:id="@+id/etWeight"
                android:layout_width="0dp"
                android:layout_weight="0.9"
                android:layout_height="wrap_content"
                android:hint="몸무게"/>

        <TextView
                android:layout_width="0dp"
                android:layout_weight="0.1"
                android:layout_height="wrap_content"
                android:text="kg"
                android:gravity="center"/>
    </LinearLayout>
</LinearLayout>

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:id="@+id/tabResult">

    <TextView
            android:id="@+id/tvResult"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text=""
            android:textSize="16sp"/>

</LinearLayout>



댓글 리스트

Created by 송바래

✉ gihun3645@naver.com

🚩경기도, 성남시