当它在 MainActivity 中工作但你想把它放在片段中时,该把代码放在哪里?

Where to put code when it works in MainActivity but you want it in a fragment?

我能够编写一个简单的计时器,用一个按钮启动和重置时间。我能够在 MainActivity 中编写它,但现在我只想在一个特定的片段上编写它。我有 3 个导航片段。家庭、历史和学习。

目前,我的计时器显示在每个选项卡上。

这是 MainActivity 中的内容

public class MainActivity extends AppCompatActivity {
private Chronometer chronometer;
private boolean running;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BottomNavigationView navView = findViewById(R.id.nav_view);
    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
            R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
    NavigationUI.setupWithNavController(navView, navController);


   chronometer = findViewById(R.id.chronometer);

}

public void startChronometer(View v) {
        if(!running) {
            chronometer.setBase(SystemClock.elapsedRealtime());
            chronometer.start();
            running = true;
            ((TextView)findViewById(R.id.startbutton)).setText("End");
        } else {
            if(running) {
                chronometer.setBase(SystemClock.elapsedRealtime());
                chronometer.stop();
                running = false;
                ((TextView) findViewById(R.id.startbutton)).setText("Start");
            }
        }
    } 
}

这里是空片段代码

public class HomeFragment extends Fragment {
    Chronometer chronometer;

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    HomeViewModel 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(getViewLifecycleOwner(), new Observer<String>() {
        @Override
        public void onChanged(@Nullable String s) {
            textView.setText(s);
        }
    });
    return root;
  }
}

感谢您的帮助和花时间阅读本文。

您可以将您的代码放在 OnViewCreated 函数中。或者简单地使用名为 'root' 的视图。为了更好地理解,我将名称更改为 viewRoot。

当然,您需要将布局放入片段中,而不是 activity。

public class HomeFragment extends Fragment {
    Chronometer chronometer;

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    HomeViewModel homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);

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


    final TextView textView = viewRoot.findViewById(R.id.text_home);
    homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
        @Override
        public void onChanged(@Nullable String s) {
            textView.setText(s);
        }
    });


    chronometer = viewRoot.findViewById(R.id.chronometer);

    //If you want to start the timer    
    startChronometer(viewRoot);

    return root;
  }
}

注意 startChronometer,因为你打电话给 findViewById。你知道这些按钮放在哪里吗? 我添加了 getView(),因此您可以从片段本身调用它们。

public void startChronometer(View v) {
        if(!running) {
            chronometer.setBase(SystemClock.elapsedRealtime());
            chronometer.start();
            running = true;
            ((TextView)getView().findViewById(R.id.startbutton)).setText("End");
        } else {
            if(running) {
                chronometer.setBase(SystemClock.elapsedRealtime());
                chronometer.stop();
                running = false;
                ((TextView) getView().findViewById(R.id.startbutton)).setText("Start");
            }
        }
    } 
}

希望能帮到你。