0

2 つの入力ボックスと 1 つの合計ボタンを備えた単純な電卓をコーディングしまし 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>

問題は、Timer コードに対して結果が得られないことですか? 私の考えによれば、テキストを3秒間表示してからテキストを表示する必要があります。私の論理も間違っている可能性があります..

4

3 に答える 3

2

E.Odebugg の回答を使用してください。

ただし、 を 3 秒間表示してから非表示にする必要がある場合は、TextViewを使用せずに次のようにしますTimerTask

textView.postDelayed(new Runnable() {
    @Override
    public void run() {
        textView.setVisibility(View.GONE);
    }
}, 3000);

TextViewデフォルトで表示され、3 秒後に非表示になります。また、 on UI スレッドpostDelayed()を実行する が使用されていることに注意してください 。Runnable

于 2013-06-28T21:32:09.270 に答える
1

したがって、loadprogressbar() のコードが間違っていると思います。そのはず:

public void loadprogressbar()
{
      // have to add code for progress bar but right now just caption text added 
      tv1.setText("This is for 3 seconds only"); 
      singleTask.schedule(new TimerTask() {
            @Override
            public void run() {

            runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                                 ///hide the progressbar here...
                              }
                            });

            }
            }, Interval);

}

そして、私があなただったら、予約語になる可能性があるため、関数の名前を「Clicked」に変更します(色もそれを示します)...

于 2013-06-28T21:33:00.717 に答える