我已经在线了,但我还没有找到从textview获取此值的方法
错误是:无法解析方法ValueOf()
time_left = timeLeftNumber.valueOf() - 1;
package com.example.sensational.timetopress;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class press_screen extends ActionBarActivity {
private int time_left;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_press_screen);
}
//Create Timer
CountDownTimer timer = new CountDownTimer(30000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
TextView amountOfTaps = (TextView) findViewById(R.id.amount_of_taps);
TextView timeLeftNumber = (TextView) findViewById(R.id.time_left_number_tv);
time_left = timeLeftNumber.valueOf() - 1;
timeLeftNumber.setText();
}
@Override
public void onFinish() {
}
};
答案 0 :(得分:0)
只需使用返回getText()
的方法CharSequence
:
time_left = timeLeftNumber.getText().toString();
为了获得Integer
值,请使用
time_left = parseInt(timeLeftNumber.getText().toString(), 10) -1;
因为它可能抛出NumberFormatException
(最终版本):
try {
time_left = parseInt(timeLeftNumber.getText().toString(), 10) -1;
} catch (NumberFormatException e) {
// handle invalid numbers; set reasonable default value
time_left = 0;
}
答案 1 :(得分:0)
你需要这样做:
time_left = Integer.parseInt(timeLeftNumber.getText()。toString()) - 1;
答案 2 :(得分:0)
TextView
没有valueOf()
方法。您可以进行以下操作:
TextView amountOfTaps = (TextView) findViewById(R.id.amount_of_taps);
TextView timeLeftNumber = (TextView) findViewById(R.id.time_left_number_tv);
time_left = Integer.valueOf(timeLeftNumber.getText().toString()) - 1;
timeLeftNumber.setText();
答案 3 :(得分:0)
要从TextView
获取价值,您应该使用getText()
方法。它会返回CharSequence
,可能很容易更改为String
,然后您必须将获取的值转换为int
time_left = Integer.parseInt(timeLeftNumber.getText().toString()
如果输入的文本不是数字,则此方法可能会抛出NumberFormatException,因此您应将此调用放在try-catch块中。