2016-12-07 124 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)); 

    } 

} 
相关问题