마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

프래그먼트 안에 있는 버튼 클릭 이벤트 처리를 어떻게 하나요?

0 추천

 

프래그먼트에 있는 시작 버튼을 누르면 타이머가 시작되게 만들려고 합니다.
다음은 메인 액티비티입니다.
package com.snailife.myapplication;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.Button;

import java.util.zip.Inflater;

public class MainActivity extends AppCompatActivity {

    Fragment first;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            Fragment fragment = null;

            switch (item.getItemId()) {
                case R.id.first_menu:
                    fragment = new FirstFragment();
                    break;
                case R.id.second_menu:
                    fragment = new SecondFragment();
                    break;
                case R.id.thrid_menu:
                    fragment = new ThirdFragment();
                    break;
            }

            getSupportFragmentManager().beginTransaction().replace(R.id.framelayout,fragment).commit();

            return true;
        }
    };

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

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        getSupportFragmentManager().beginTransaction().add(R.id.framelayout,new FirstFragment()).commit();
        
    }

}
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigation"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        android:layout_alignParentBottom="true"
        app:menu="@menu/navigation" />

</RelativeLayout>
firstfragment가 화면에 뜨고, 시작 버튼을 눌렀을때 타이머가 작동되게 만들려고 하는데, 버튼을 findviewbyid를 어디서 어떻게 해야 하나요? 검색해보니 
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment); 이런게 있던데, 프래그먼트를 받아서 그 다음에 어떻게 해야하는지 모르겠습니다.
배경색이 왜 검은색으로 나오는지 모르겠네요.. 스승님들 도움이 필요합니다!
제자 (140 포인트) 님이 2019년 4월 7일 질문
...