无法在if语句之外使用String变量

时间:2016-10-15 07:10:53

标签: java android android-intent

我已经实现了这样的计时器(在代码中提到)。我更改了它的字符串格式以便hours:minutes:seconds,对我来说,使用开关,我必须添加一个if(){}条件语句。

所以我做了,我必须在if(){}内声明字符串,因为我无法在if(){}之外进行。但是现在我想使用该字符串信息来打开一个新活动,并将字符串值分配给 TextView

问题是我无法使用if(){}之外的字符串,首先我将 hms 字符串与一起使用TextView 称为 myText

问题是myText TextView在第一个活动中完美运行,但在使用 Inetnt 将信息发送到另一个活动中的另一个 Textview 时,它显示没有任何变化的的TextView 即可。

Heres是代码:

 if (mySwitch.isChecked()) {
                        int getvaluehour = numPickerHour.getValue();
                        int getvalueminute = numPickerMin.getValue();

                        getvaluehour = getvaluehour * 3600000;
                        getvalueminute = getvalueminute * 60000;
                        long hoursandMinstomils = getvalueminute + getvaluehour;

                        new CountDownTimer(hoursandMinstomils, 1000) {

                            public void onTick(long millisUntilFinished) {
                                //here you can have your logic to set text to edittext
                                long millis = millisUntilFinished;
                                //Convert milliseconds into hour,minute and seconds
                                 String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
                                myText.setText(hms);

                            }

                            public void onFinish() {

                                myText.setText("TIME'S UP!!"); //On finish change timer text
                            }
                        }.start();

以及我如何使用 Intent Activity1:

Intent toST = new Intent(MainActivity.this, ShowTime.class);

                        String textMessage = myText.getText().toString() ;
                        toST.putExtra("wargra", textMessage);

                        startActivity(toST);

以及我在activity2中收到它的方式:

Bundle receiver = getIntent().getExtras();
        if(receiver == null){
            return;
        }

        String textMessage = reciver.getString("wargra");
        myText2.setText(textMessage);

如何将hms值发送到myText2?

以下是完整的activity1代码:

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Switch;
import android.widget.TextView;

import java.util.concurrent.TimeUnit;


public class MainActivity extends AppCompatActivity {


    Button silentButton;
    AudioManager myAudioManager;
    NumberPicker numPickerHour;
    NumberPicker numPickerMin;
    Switch mySwitch;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Text,Audio,Button,Time

        myAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        silentButton = (Button) findViewById(R.id.Start);

        //Number Picker
        numPickerHour = (NumberPicker) findViewById(R.id.numberPickerHour);
        numPickerMin = (NumberPicker) findViewById(R.id.numberPickerMinute);
        mySwitch = (Switch) findViewById(R.id.Toggle);

        //Hour
        numPickerHour.setMaxValue(24);
        numPickerHour.setMinValue(0);
        numPickerHour.setWrapSelectorWheel(true);
        //Min
        numPickerMin.setMaxValue(60);
        numPickerMin.setMinValue(0);
        numPickerMin.setWrapSelectorWheel(true);



        silentButton.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View v) {


                        final TextView myText;
                        myText = (TextView)findViewById(R.id.timeText);



                        if (mySwitch.isChecked()) {
                            int getvaluehour = numPickerHour.getValue();
                            int getvalueminute = numPickerMin.getValue();

                            getvaluehour = getvaluehour * 3600000;
                            getvalueminute = getvalueminute * 60000;
                            long hoursandMinstomils = getvalueminute + getvaluehour;

                            new CountDownTimer(hoursandMinstomils, 1000) {

                                public void onTick(long millisUntilFinished) {
                                    //here you can have your logic to set text to edittext
                                    long millis = millisUntilFinished;
                                    //Convert milliseconds into hour,minute and seconds
                                     String hms = String.format("Viberation end in: " + "%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
                                    myText.setText(hms);


                                    //set text

                                    myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

                                }

                                public void onFinish() {
                                    myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                                    myText.setText("TIME'S UP!!"); //On finish change timer text
                                }
                            }.start();

                        } else {
                            int getvaluehour = numPickerHour.getValue();
                            int getvalueminute = numPickerMin.getValue();

                            getvaluehour = getvaluehour * 3600000;
                            getvalueminute = getvalueminute * 60000;
                            long hoursandMinstomils = getvalueminute + getvaluehour;

                            new CountDownTimer(hoursandMinstomils, 1000) {

                                public void onTick(long millisUntilFinished) {
                                    //here you can have your logic to set text to edittext
                                    long millis = millisUntilFinished;
                                    //Convert milliseconds into hour,minute and seconds
                                    String hms = String.format("Silent ends in (" + "%02d:%02d:%02d" + ")", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
                                    myText.setText(hms);//set text

                                    myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

                                }

                                public void onFinish() {
                                    myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                                    myText.setText("TIME'S UP!!"); //On finish change timer text
                                }
                            }.start();
                        }


                        Intent toST = new Intent(MainActivity.this, ShowTime.class);

                        String textMessage = myText.getText().toString() ;
                        toST.putExtra("wargra", textMessage);

                        startActivity(toST);

                    }
                }
        );


                        }
                    }

2 个答案:

答案 0 :(得分:0)

final TextView myText;
myText = (TextView)findViewById(R.id.timeText);

更改这两行。全局声明myText(正如您已经使用silentButton,myAudioManager,numPickerHour,numPickerMin,mySwitch)。

public class MainActivity extends AppCompatActivity {

    //Following your other code
    ....

    TextView myText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Following your other code
        ....

        myText = (TextView)findViewById(R.id.timeText);

        //Rest of your code
        ....

    }
}

希望你的问题不复存在。

答案 1 :(得分:0)

可能有两个原因和解决方案:

案例1 :如果您在线程中使用if,则应更新TextView上的UIThread,即MainThread

像这样:

if (mySwitch.isChecked()) {
                    int getvaluehour = numPickerHour.getValue();
                    int getvalueminute = numPickerMin.getValue();

                    getvaluehour = getvaluehour * 3600000;
                    getvalueminute = getvalueminute * 60000;
                    long hoursandMinstomils = getvalueminute + getvaluehour;

                    new CountDownTimer(hoursandMinstomils, 1000) {

                        public void onTick(long millisUntilFinished) {
                            //here you can have your logic to set text to edittext
                            long millis = millisUntilFinished;
                            //Convert milliseconds into hour,minute and seconds
                             String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
                            MainActivity.this.runOnUIThread(new Runnable()
                                 {
                                 @Override
                                 public void run()
                                    {
                                    myText.setText(hms);
                                    }
                                 }).start();

                        }

                        public void onFinish() {
                            MainActivity.this.runOnUIThread(new Runnable()
                                 {
                                 @Override
                                 public void run()
                                    {
                                    myText.setText("TIME'S UP!!"); //On finish change timer text
                                    }
                                 }).start();

                        }
                    }.start();

您应该始终只使用runOnUIThread()方法更新MainThread上的任何UI元素。可能这是你问题的原因。

案例2: Sudip Podder提供的解决方案。

案例3 :回答您的问题: 在java中,您不能只使用其范围之外的变量。你可以做的就是将值放到类变量中,如下所示: 只需声明一个类变量并将值放入其中,然后使用类变量作为值。

public class MainActivity extends AppCompatActivity {
....
....
String time;
....
....
}
....
....
....
if (mySwitch.isChecked()) {
                int getvaluehour = numPickerHour.getValue();
                int getvalueminute = numPickerMin.getValue();

                getvaluehour = getvaluehour * 3600000;
                getvalueminute = getvalueminute * 60000;
                long hoursandMinstomils = getvalueminute + getvaluehour;

                new CountDownTimer(hoursandMinstomils, 1000) {

                    public void onTick(long millisUntilFinished) {
                        //here you can have your logic to set text to edittext
                        long millis = millisUntilFinished;
                        //Convert milliseconds into hour,minute and seconds
                         String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
                        MainActivity.this.time=hms;
                        MainActivity.this.runOnUIThread(new Runnable()
                             {
                             @Override
                             public void run()
                                {
                                myText.setText(MainActivity.this.time);
                                }
                             }).start();

                    }

                    public void onFinish() {
                        MainActivity.this.runOnUIThread(new Runnable()
                             {
                             @Override
                             public void run()
                                {
                                myText.setText("TIME'S UP!!"); //On finish change timer text
                                }
                             }).start();

现在您可以通过类变量time使用hms变量的值。是的,不要忘记将myText声明为类变量。