我在片段和按钮点击中有2个按钮,它们显示Toast但我想移动到另一个活动。
这是我的代码......
public class HomeFragment extends Fragment implements View.OnClickListener {
Button btn,btn2;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_home,
container, false);
btn = (Button) view.findViewById(R.id.btn);
btn2 = (Button) view.findViewById(R.id.btn2);
btn.setOnClickListener(this);
btn2.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn:
Toast.makeText(this.getActivity(),
"Button 1!", Toast.LENGTH_LONG).show();
break;
case R.id.btn2:
Toast.makeText(this.getActivity(),
"Button 2 !", Toast.LENGTH_LONG).show();
break;
}
}
}
答案 0 :(得分:1)
**Replace these lines in place of toast and run your code:**
Intent intent = new Intent(getActivity(),YourDestinationActivity.class);
startActivity(intent);
答案 1 :(得分:0)
使用Intent进行导航
替换:
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn:
Intent intent = new Intent(getActivity(),YourDestinationActivity.class);
startActivity(intent);
break;
case R.id.btn2:
Intent intent = new Intent(getActivity(),YourDestinationActivity.class);
startActivity(intent);
break;
}
}