【问题标题】:Going to a new page, by pressing a button from a fragment on Andriod studio通过按下 Android Studio 中的片段中的按钮进入新页面
【发布时间】:2020-05-28 12:52:27
【问题描述】:

这是一个非常简单的问题,但是如何从按下片段上的按钮移动到新页面?

一旦按下按钮似乎没有做任何事情,没有任何错误。

这里是 Java 代码 -

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    Button btn = (Button) view.findViewById(R.id.help_button_ID);
    btn.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view){
            Intent intent = new Intent (getActivity(), help.class);



        }
    });

}

private HomeViewModel homeViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
                        ViewGroup container, Bundle savedInstanceState) {



  homeViewModel =
           ViewModelProviders.of(this).get(HomeViewModel.class);
   View root = inflater.inflate(R.layout.fragment_home, container, false);
   final TextView textView = root.findViewById(R.id.text_home);
   homeViewModel.getText().observe(this, new Observer<String>() {
       @Override
     public void onChanged(@Nullable String s) {
           textView.setText(s);
        }
    });

    return root;

} }

【问题讨论】:

    标签: android button fragment


    【解决方案1】:

    您正在创建一个 Intent,但您没有开始该活动。

    https://developer.android.com/training/basics/firstapp/starting-activity

    【讨论】:

    • 为回复欢呼。遵循该指南后,按钮仍然没有转到所需页面,这是我添加的代码... startActivity(new Intent(getActivity(), help.class)); getActivity().finish();
    • 1 - 在监听器上记录一些东西,确保它触发。 2 - Intent构造函数的第二个参数是一个类,大写Help.class
    猜你喜欢
    • 2020-06-19
    • 2012-12-17
    • 1970-01-01
    • 2013-04-21
    • 2016-05-31
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多