나는 스피너와 버튼으로 구성된 프래그먼트를 가지고 있습니다. 회 전자로 네 가지 옵션 중 하나를 선택하면 단추가 다음 활동으로 이동합니다.동일한 프래그먼트에 여러 이벤트 리스너 구현 - 안드로이드
회 전체를 구현하려면 조각에서 onItemSelectedListener를 구현해야하지만 버튼을 사용하려면 onClickListener를 구현해야합니다. 하지만 둘 다 할 수 없습니까 ??? 나는 이것이 정말 간단한 것일 것이고 View에 여러 개의 다른 이벤트 리스너를 가질 필요가 있다고 생각 했겠지만, 이것을 어떻게 구현합니까?
공용 클래스 FragmentTypeSelect 조각을 구현 OnItemSelectedListener {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState){
// set the view so that it can be referenced
View theView = inflater.inflate(R.layout.fragment_type_select,
container,false);
// set OnClickListener for the button
setUpClickListener(theView,R.id.but_select);
//===============================
// NEW TYPE SPINNER
//===============================
Spinner spinner = (Spinner) theView.findViewById(R.id.new_type_spinner);
spinner.setOnItemSelectedListener((OnItemSelectedListener) this);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.types_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
return theView;
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id)
{
TextView headingText = (TextView)getView().findViewById(R.id.new_diet_type_text_heading);
TextView detailText = (TextView)getView().findViewById(R.id.new_diet_type_text_detail);
if (pos == 0)
{
headingText.setText(R.string.heading_type_1);
detailText.setText(R.string.detail_type_1);
}
if (pos == 1)
{
headingText.setText(R.string.heading_type_2);
detailText.setText(R.string.detail_type_2);
}
if (pos == 2)
{
headingText.setText(R.string.heading_type_3);
detailText.setText(R.string.detail_type_3);
}
if (pos == 3)
{
headingText.setText(R.string.heading_type_4);
detailText.setText(R.string.detail_type_4);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public void onClick(View view){
Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_LONG).show();
}
private void setUpClickListener(View theView, int childViewID) {
View childView = theView.findViewById(childViewID);
childView.setOnClickListener((OnClickListener) this);
}
}
원래난 그냥에서 스피너를했고이있어 확장 -이 : 여기
내가 사용하고있는 코드입니다 잘 작동합니다. 그런 다음 onCreateView에 OnClickListener 세트가있는 버튼 기능을 추가하고 추가 onClick 및 setUpClickListener 메소드를 추가하려고했습니다. 이것은 정확히 내가 다른 곳에서 해낸 방법이지만 다른 경우에는 처리 할 다른 이벤트가 없었고 클래스에서 onClickListener를 구현하도록 만들었습니다. Java는 여러 인터페이스 도구 (이해할 수있는)를 지원하지 않으므로 나의 질문이됩니다.희망 하시겠습니까? 나는 약간 두꺼운지만 아마 안드로이드뿐만 아니라 전체 OO에 여전히 상당히 익숙하다.
당신은 둘 다 할 수 있습니다. 코드를 게시하고 문제를 더 자세히 설명하십시오. –
레오, 위의 코드를 게시했습니다 .... 감사합니다. – RobT