package com.example.gradecal;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;
public class FstFrag extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sub_1, container, false);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this.getActivity(), R.array.grade,
android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(
this.getActivity(), R.array.score,
android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner g_Sp1 = (Spinner) rootView.findViewById(R.id.G_Spin1);
g_Sp1.setAdapter(adapter1);
Spinner g_Sp2 = (Spinner) rootView.findViewById(R.id.G_Spin2);
g_Sp2.setAdapter(adapter1);
Spinner g_Sp3 = (Spinner) rootView.findViewById(R.id.G_Spin3);
g_Sp3.setAdapter(adapter1);
Spinner g_Sp4 = (Spinner) rootView.findViewById(R.id.G_Spin4);
g_Sp4.setAdapter(adapter1);
Spinner s_Sp1 = (Spinner) rootView.findViewById(R.id.S_Spin1);
s_Sp1.setAdapter(adapter2);
Spinner s_Sp2 = (Spinner) rootView.findViewById(R.id.S_Spin2);
s_Sp2.setAdapter(adapter2);
Spinner s_Sp3 = (Spinner) rootView.findViewById(R.id.S_Spin3);
s_Sp3.setAdapter(adapter2);
Spinner s_Sp4 = (Spinner) rootView.findViewById(R.id.S_Spin4);
s_Sp4.setAdapter(adapter2);
CheckBox chk1 = (CheckBox) rootView.findViewById(R.id.chk1);
chk1.setOnCheckedChangeListener(new MyCheckListener(chk1, g_Sp1, s_Sp1));
CheckBox chk2 = (CheckBox) rootView.findViewById(R.id.chk2);
chk2.setOnCheckedChangeListener(new MyCheckListener(chk2, g_Sp2, s_Sp2));
CheckBox chk3 = (CheckBox) rootView.findViewById(R.id.chk3);
chk3.setOnCheckedChangeListener(new MyCheckListener(chk3, g_Sp3, s_Sp3));
CheckBox chk4 = (CheckBox) rootView.findViewById(R.id.chk4);
chk4.setOnCheckedChangeListener(new MyCheckListener(chk4, g_Sp4, s_Sp4));
Button excute = (Button) rootView.findViewById(R.id.excute_btn);
Button save = (Button) rootView.findViewById(R.id.save_btn);
return rootView;
}
class MyCheckListener implements OnCheckedChangeListener {
private CheckBox mChk;
private Spinner mSpin1, mSpin2;
public MyCheckListener(CheckBox chk, Spinner spin1, Spinner spin2) {
mChk = chk;
mSpin1 = spin1;
mSpin1 = spin2;
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (mChk.isChecked()) {
mSpin1.setClickable(true);
mSpin2.setClickable(true);
}
else {
mSpin1.setClickable(false);
mSpin2.setClickable(false);
}
}
}
}
학점계산기를 만들면서 체크박스가 체크되있으면 스피너를 활성시키고 비활성 상태라면 스피너를 비활성 시키기 위해
체크박스에 setOnCheckedChangeListener 를 추가해주고 밑에서 제가 만든 리스너를 불러왔는데
뭐가 문제인지 체크박스를 클릭하면 앱이 자꾸 꺼지네요;
저번에 인터넷에서 프래그먼트는 메소드를 추상메소드만 만들고 엑티비티에서 정의해야한다고 본거 같은데
프레그먼트에서는 메소드를 쓰면 안되는 건가요?
답변 부탁드립니다. 그리고 혹시 코드중에 이상한 점이 있으면 알려주세요 ㅠㅠ
- 추가 -
따로 클래스를 만들어서 안하고
무기명 이너클래스로 만드니까 동작하네요?
chk1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView.getId() == chk1.getId()) {
if (isChecked) {
g_Sp1.setClickable(true);
s_Sp1.setClickable(true);
}
else {
g_Sp1.setClickable(false);
s_Sp1.setClickable(false);
}
}
}
});
어쩌란건지 ㅠㅠ