我用两个输入框和一个总按钮编写了简单的计算器,我要做的是显示一个 circular Progressbar for 3 seconds before i show result after pressing Total button
,这就是我所做的。
包 com.example.calculator;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity
{
EditText t1;
EditText t2;
TextView tv;
TextView tv1;
Timer singleTask;
int Interval = 3000;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1= (EditText)findViewById(R.id.editText1);
t2= (EditText)findViewById(R.id.editText2);
tv= (TextView)findViewById(R.id.textView1);
tv1= (TextView)findViewById(R.id.textView2);
singleTask= new Timer();
}
public void loadprogressbar()
{
singleTask.schedule(new TimerTask() {
@Override
public void run() {
// have to add code for progress bar but right now just caption text added
tv1.setText("This is for 3 seconds only");
}
}, 1000);
}
@Override
protected void onDestroy(){
super.onDestroy();
if(singleTask != null)
{
singleTask.cancel();
}
}
public void Clicked(View v)
{ int total;
if(v.getId()==R.id.button1)
{
int v1 = Integer.parseInt(t1.getText().toString());
int v2 = Integer.parseInt(t2.getText().toString());
total = v1 + v2;
loadprogressbar();
tv.setText(total+"");
tv.setVisibility(1);
}
else if (v.getId()==R.id.button2)
{
t1.setText("");
t2.setText("");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
XML 文件是
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:visibility="invisible"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
></TextView>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="21dp"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/editText2"
android:text="Clear"
android:onClick="Clicked"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText2"
android:layout_marginTop="35dp"
android:text="Total"
android:onClick="Clicked"
/>
</RelativeLayout>
问题是我没有得到任何针对计时器代码的结果?根据我的想法,它应该显示文本 3 秒,然后显示文本。我的逻辑也可能是错误的,请指导我..