dingdaoan 2021-05-07 14:01 采纳率: 0%
浏览 90

虚拟机运行app提示停止运行?

API21

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/timeview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/TIME"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textSize="60sp" />

        <Button
            android:id="@+id/buttonstart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:onClick="onClickstart"
            android:text="@string/start" />

        <Button
            android:id="@+id/buttonstop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:onClick="onClickstop"
            android:text="@string/stop" />

        <Button
            android:id="@+id/buttonreset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:onClick="onClickreset"
            android:text="@string/reset" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

string.xml

<resources>
    <string name="app_name">stopwatch</string>
    <string name="TIME">0:00;00</string>
    <string name="start">start</string>
    <string name="stop">stop</string>
    <string name="reset">reset</string>
</resources>

Mainactivity.java

package com.e.stopwatch;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private int seconds=0;
    private boolean running=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        runtimer();
    }
    public void onClickstart(View view){running=true;}
    public void onClickstop(View view){running=false;}
    public void onClickreset(View view){running=false;seconds=0;}
    private void runtimer(){
        final TextView timeview=findViewById(R.id.timeview);
        final Handler handler=new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                int hours=seconds/3600;
                int minutes=(seconds%3600)/60;
                int secs=seconds%60;
                String time=String.format("%d:%02d:02d%",hours,minutes,secs);
                timeview.setText(time);
                if (running){seconds++;}
                handler.postDelayed(this,1000);

            }
        });

    }
}
  • 写回答

5条回答 默认 最新

  • CSDN专家-Time 2021-05-07 14:03
    关注

    是不是哪里写错了

    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法