如何创建一个Android移动应用程序,它将以毫秒为单位进行计数(从0开始),并要求用户输入"我喜欢吃#34;在EditText中单击“提交”。用户点击提交后,计数将停止,并出现一个对话框并显示计数结果。
package com.example.labex4;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView textView1,textView2,textView3,textView4,displayValue;
EditText nameTxt;
Button button1;
int count = 0;
Timer T;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
T=new Timer();
T.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable()
{
@Override
public void run()
{
displayValue.setText("count="+count);
count++;
}
});
}
}, 0, 1);
textView1 = (TextView)findViewById(R.id.textView1);
textView2 = (TextView)findViewById(R.id.textView2);
textView3 = (TextView)findViewById(R.id.textView3);
textView4 = (TextView)findViewById(R.id.textView4);
displayValue = (TextView)findViewById(R.id.displayValue);
nameTxt = (EditText)findViewById(R.id.nameTxt);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
T.cancel();
dialog.editext.setText(""+count);
}
}
***Here is The XML file***
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="(Type Faster)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="25dp"
android:text="Miliseconds: " />
<TextView
android:id="@+id/displayValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:text="displayvalue" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="Type this words: " />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="Frankie Jones Anak Saing"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/nameTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView4"
android:layout_marginTop="38dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameTxt"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:text="Check" />
</RelativeLayout>
答案 0 :(得分:0)
全局变量
int count = 0;
Timer T;
在setContentView
之后将以下代码放在onCreate中T=new Timer();
T.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable()
{
@Override
public void run()
{
//myTextView.setText("count="+count);
count++;
}
});
}
}, 0, 1);
在Button OnCLick
<强>更新强>
T.cancel();
//Open popup or dialog or alert as per your choice and print the count value
//dialog.editext.setText(""+count);
AlertDialog alertDialog = new AlertDialog.Builder(<YourActivityName>this).create(); //Read Update
alertDialog.setTitle("You total time count");
alertDialog.setMessage(String.valueof(count));
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
alertDialog.dismiss();
}
});
alertDialog.show(); //<-- See This!
}
根据您的要求,我正在粘贴全班
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends Activity implements OnClickListener {
TextView textView1,textView2,textView3,textView4,displayValue;
EditText nameTxt;
Button button1;
int count = 0;
Timer T;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
T=new Timer();
T.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable()
{
@Override
public void run()
{
displayValue.setText("count="+count);
count++;
}
});
}
}, 0, 1);
textView1 = (TextView)findViewById(R.id.textView1);
textView2 = (TextView)findViewById(R.id.textView2);
textView3 = (TextView)findViewById(R.id.textView3);
textView4 = (TextView)findViewById(R.id.textView4);
displayValue = (TextView)findViewById(R.id.displayValue);
nameTxt = (EditText)findViewById(R.id.nameTxt);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
T.cancel();
//Open popup or dialog or alert as per your choice and print the count value
//dialog.editext.setText(""+count);
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.custom_popup);
dialog.setTitle("Typing Result!");
// set the custom dialog components - text, image and button
TextView titletext = (TextView) dialog.findViewById(R.id.titletext);
//titletext.setText("Typing Result!");
titletext.setVisibility(View.GONE);
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Frankie Jones Igat, Your Typing Speed "+String.valueOf(count)+" miliseconds");
Button dialogButtonOK = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButtonOK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
自定义弹出布局文件夹custom_popup.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#999999"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/titletext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="title"
android:textSize="18sp"
android:textColor="@android:color/black"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/titletext"
android:layout_marginTop="5dp"
android:textSize="16sp"
android:text="titlekiefsjisejefsjeaij"
android:textColor="@android:color/black"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="80dp"
android:layout_height="40dp"
android:text=" Ok "
android:layout_marginTop="10dp"
android:layout_marginRight="5dp"
android:layout_below="@+id/text"
android:textSize="18sp"
android:textColor="@android:color/black"
android:layout_centerHorizontal="true"
android:gravity="center"
android:background="#FF0000"
android:layout_marginBottom="10dp"
/>
</RelativeLayout>