Android 中 if 语句中的整数值

value of ints in if statements in Android

所以在我的 class 中我有一个录音功能(2 个按钮,开始和停止录音)和一个留下你的名字的功能(文本编辑)。这是我的调查应用程序的第二个屏幕,我希望如果用户在第二个屏幕上 20 秒未提供任何输入,应用程序将跳回到第一个屏幕。

public class Name extends Activity {

Button mButton;
EditText mEdit;
String name;
int a = 0;   //AudioRecorder starts here 
private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private MediaRecorder recorder = null;
private int currentFormat = 0;
private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.OutputFormat.THREE_GPP };
private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP };

private void setButtonHandlers() {
    ((Button) findViewById(R.id.btnStart)).setOnClickListener(btnClick);
    ((Button) findViewById(R.id.btnStop)).setOnClickListener(btnClick);

}
private void enableButton(int id, boolean isEnable) {
    ((Button) findViewById(id)).setEnabled(isEnable);
}
private void enableButtons(boolean isRecording) {
    enableButton(R.id.btnStart, !isRecording);
    enableButton(R.id.btnStop, isRecording);
}

private String getFilename() {
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath, AUDIO_RECORDER_FOLDER);
    if (!file.exists()) {
        file.mkdirs();
    }
    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + file_exts[currentFormat]);
}

private void startRecording() {
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(getFilename());
    recorder.setOnErrorListener(errorListener);
    recorder.setOnInfoListener(infoListener);
    try {
        recorder.prepare();
        recorder.start();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
private void stopRecording() {
    if (null != recorder) {
        recorder.stop();
        recorder.reset();
        recorder.release();
        recorder = null;
    }
}
private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    @Override
    public void onError(MediaRecorder mr, int what, int extra) {
        Toast.makeText(Name.this, "Error: " + what + ", " + extra, Toast.LENGTH_SHORT).show();
    }
};

private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {
        Toast.makeText(Name.this, "Warning: " + what + ", " + extra, Toast.LENGTH_SHORT).show();
    }
};

private View.OnClickListener btnClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnStart: {
                Toast.makeText(Name.this, "Start Recording", Toast.LENGTH_SHORT).show();
                enableButtons(true);
                startRecording();
                a=a+1;
                break;
            }
            case R.id.btnStop: {
                Toast.makeText(Name.this, "Stop Recording", Toast.LENGTH_SHORT).show();
                enableButtons(false);
                stopRecording();
                a=a-1;
                break;
            }
        }
    }
};


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.name);

    // getting the name
    mButton = (Button) findViewById(R.id.button2);
    mEdit = (EditText)findViewById(R.id.editText);

    mButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            name = mEdit.getText().toString();
           //sending the name to an email address
            new Thread(new Runnable() {
                public void run() {
                    try {
                        GMailSender sender = new GMailSender(
                                "mailaddress",
                                "PW");
                       sender.sendMail(name, name + " made the last input",
                                "mailaddress",
                                "mailaddress");
                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
                    }
                }
            }).start();
            startActivity(new Intent("android.intent.action.THANKYOU"));
            if (a==1){
                Toast.makeText(Name.this, "Stop Recording", Toast.LENGTH_SHORT).show();
                enableButtons(false);
                stopRecording();
            }
        }
    });

    Handler handler = new Handler();
    Runnable r = new Runnable() {
        public void run() {
            if (mEdit.getText().toString().length() == 0 &&a==0) {
                startActivity(new Intent("android.intent.action.THANKYOU"));
            }
        }
    };
    handler.postDelayed(r, 2000);
//Audio Record
    setButtonHandlers();
    enableButtons(false);
}}

所以我的问题是,如果我按下开始和停止按钮,a 应该是 0,因此这个 if 语句应该出现

if (mEdit.getText().toString().length() == 0 &&a==0)

但是,一旦我按下开始按钮,之后我按下什么都没有关系,if 语句再也不会跳入。

有什么想法吗?

您的处理程序代码应位于 onResume() 块中,以便每次显示视图时都会调用它。我不确定您在 post 中所说的延迟值“2000”是 2 秒还是“20000”是 20 秒。当我将时间延长到 20 秒并进行一些测试时,一旦我将 Handler 块移出,一切正常,如下所示:

    public void onResume(){
        super.onResume();
        Handler handler = new Handler();
        Runnable r = new Runnable() {
            public void run() {
                if (mEdit.getText().toString().length() == 0 && a==0) {
                    startActivity(new Intent("android.intent.action.THANKYOU"));
                }
            }
        };
        handler.postDelayed(r, 20000);
    }