2016-12-07 120 views
0

目前我有一個fragment_one.xml其上有5 CardViews,並且每個卡片上都有一個Button,用於分離XML頁面(Lesson_One,Lesson_Two等等...) )但與我在OneFragment.java代碼,這兩個按鈕都是開放的Lesson_Two按鈕都從一個Android片段打開相同的活動

我該如何解決這個問題?這裏是我的代碼

FragmentOne.java

public class OneFragment extends Fragment{ 
    Intent intent; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View root = inflater.inflate(R.layout.fragment_one, container, false); 
    intent = new Intent(getActivity(), LessonOne.class); 
    final Button button = (Button) root.findViewById(R.id.button1); 

    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      startActivity(intent); 
     } 
    }); 

    intent = new Intent(getActivity(), LessonTwo.class); 
    final Button button2 = (Button) root.findViewById(R.id.button2); 

    button2.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      startActivity(intent); 
     } 
    }); 
    return root; 
    } 

} 

回答

3

你分配intent兩次,有效覆蓋與第二第一意圖。

所以不管觸發哪個點擊事件LessonTwo.class是啓動的活動。

一個簡單的解決方法是創建點擊處理程序內部的意圖像

public class OneFragment extends Fragment{ 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View root = inflater.inflate(R.layout.fragment_one, container, false); 
    final Button button = (Button) root.findViewById(R.id.button1); 

    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      startActivity(new Intent(getActivity(), LessonOne.class)); 
     } 
    }); 

    final Button button2 = (Button) root.findViewById(R.id.button2); 

    button2.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      startActivity(new Intent(getActivity(), LessonTwo.class);); 
     } 
    }); 

    return root; 
    } 

} 

這使得明確的點擊處理開始這是什麼活動

+0

完美!謝謝一堆,我感謝它! –

1

備選答案 - 實現類本身的點擊監聽器。

這清理了onCreateView方法。你也不需要「捕捉」按鈕來設置他們的聽衆。

public class OneFragment extends Fragment implements View.OnClickListener { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
     View root = inflater.inflate(R.layout.fragment_one, container, false); 

     root.findViewById(R.id.button1).setOnClickListener(this); 
     root.findViewById(R.id.button2).setOnClickListener(this); 

     return root; 
    } 

    @Override 
    public void onClick(View v) { 
     Class clz = null; 
     switch (v.getId()) { 
      case R.id.button1: 
       clz = LessonOne.class; 
      case R.id.button2; 
       clz = LessonTwo.class; 
     } 

     if (clz != null) startActivity(new Intent(getActivity(), clz)); 

    } 

} 
相關問題