在android studio java语言中用一个按钮获得开始和停止时间

0 人关注

我做了两个按钮,当点击开始按钮时,它在文本视图中显示当前时间。当点击停止按钮时,它也在文本视图中显示当前时间。 我的问题是,当我点击按钮上的多个时间时,它持续改变时间。我希望通过一次点击就能得到开始和停止的时间。我的意思是,我有一个按钮,当点击该按钮时,它在文本视图中显示当前的开始时间,并将该按钮的文本转为停止,然后当再次点击该按钮时,显示停止时间,并计算两个时间的差异并显示在文本视图中。

public class Aircraft extends Fragment {
    TextView tvstarttime;
    Button btn_start;
    Button btn_end;
    TextView tvend;
    TextView diffence;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_aircraft, container, false);
        tvstarttime = view.findViewById(R.id.txtstarttime);
        btn_start = view.findViewById(R.id.btn_start);
        btn_end= view.findViewById(R.id.btn_end);
        tvend = view.findViewById(R.id.txtendtime);
        diffence=view.findViewById(R.id.txtdifference);
       btn_start.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Calendar calendar = Calendar.getInstance();
               SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
              String time1 = format.format(calendar.getTime());
               tvstarttime.setText(time1);
        btn_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
                String time = format.format(calendar.getTime());
                tvend.setText(time);
                try {
                    Date date1 = format.parse(tvend.getText().toString());
                    Date date2 = format.parse(tvstarttime.getText().toString());
                    long mills = date1.getTime() - date2.getTime();
                    Log.v("Data1", ""+date1.getTime());
                    Log.v("Data2", ""+date2.getTime());
                    int hours = (int) (mills/(1000 * 60 * 60));
                    int mins = (int) (mills % (1000*60*60));
                    String diff = hours + ":" + mins;
                    diffence.setText(diff);
                } catch (ParseException e) {
                    e.printStackTrace();
        return view;
    
3 个评论
考虑扔掉早已过时的、臭名昭著的麻烦的 SimpleDateFormat 和朋友。用 脱脂 in order to use java.time,现代Java日期和时间API . It is so much nicer to work with.
是的,你能帮助我根据条件编辑我的代码吗?它显示开始和停止时间,并作出差异,我想显示四个时间,进入,开始,停止和退出时间的同一按钮,并计算开始和停止时间的差异。
java
android
sql
android-studio
Muhammad Ali
Muhammad Ali
发布于 2022-04-26
1 个回答
Arkaprabha Mahata
Arkaprabha Mahata
发布于 2022-04-26
已采纳
0 人赞同

public class Aircraft extends Fragment {

TextView tvstarttime;
Button btn_start;
TextView tvend;
TextView diffence;
boolean isStarted = false;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_aircraft, container, false);
    tvstarttime = view.findViewById(R.id.txtstarttime);
    btn_start = view.findViewById(R.id.btn_start);
    tvend = view.findViewById(R.id.txtendtime);
    diffence=view.findViewById(R.id.txtdifference);
    btn_start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isStarted){
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
                String time = format.format(calendar.getTime());
                tvend.setText(time);
                try {
                    Date date1 = format.parse(tvend.getText().toString());
                    Date date2 = format.parse(tvstarttime.getText().toString());
                    long mills = date1.getTime() - date2.getTime();
                    Log.v("Data1", ""+date1.getTime());
                    Log.v("Data2", ""+date2.getTime());
                    int hours = (int) (mills/(1000 * 60 * 60));
                    int mins = (int) (mills % (1000*60*60));
                    String diff = hours + ":" + mins;
                    diffence.setText(diff);
                } catch (ParseException e) {
                    e.printStackTrace();
                btn_start.setText("START");
            }else {
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
                String time1 = format.format(calendar.getTime());
                tvstarttime.setText(time1);
                btn_start.setText("STOP");
            isStarted = !isStarted;