gpt4 book ai didi

java - 通过单击操作按钮更改 Activity ?

转载 作者:行者123 更新时间:2023-12-01 23:11:03 25 4
gpt4 key购买 nike

如何使用操作按钮更改 Activity ?

package com.com.com;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class HomeFragment extends Fragment {

public HomeFragment(){}

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

//In order for this method to receive calls
setHasOptionsMenu(true);
}

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

View rootView = inflater.inflate(R.layout.fragment_home, container, false);

return rootView;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_new1:
Button switchButton = (Button) findViewById(R.id.btnsignin);


switchButton.setOnClickListener(new View.OnClickListener() {


@Override

public void onClick(View v) {

Intent intent = new Intent(HomeFragment.this, Login.class);

startActivity(intent);

}

});


}
return true;
default:
return super.onOptionsItemSelected(item);
}
}

这是我当前的代码,但它似乎不起作用。有人可以解释一下如何让它工作吗?我知道我需要这个

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_new1:
openSearch();
return true;
default:
return super.onOptionsItemSelected(item);
}

但如果我需要将 openSearch 替换为转换代码,我就不会这样做。任何帮助都会很棒。

最佳答案

为什么你需要使用这个按钮!

这必须在没有任何类似按钮的情况下工作:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_new1:

Intent intent = new Intent( getActivity(), Login.class);
getActivity().startActivity(intent);

return true;
default:
return super.onOptionsItemSelected(item);
}
}

关于java - 通过单击操作按钮更改 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21971831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com