課題でandroidstudioを用いてカウントダウンタイマーを作っています。
edittextを使ってユーザーが時間をセットできるようにしたいです。
よろしくお願いします。
発生している問題・エラーメッセージ
setButtonを押すとcountNumberに入力した数値がるようにしたいのですが //タイマーに時間をセットするの//エラーのコードにfinalを宣言する必要が あるとえらーが出ます、
<code>java
1public class TimerActivity extends AppCompatActivity { 2 3 private TextView timerText; 4 private EditText edit; 5 private SharedPreferences data; 6 7 8 // 表示形式を設定 9 private SimpleDateFormat dataFormat = new SimpleDateFormat("mm:ss.SSS"); 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.my_fragment); 15 16 edit= (EditText)findViewById(R.id.time); 17 18 19 //タイマーの初期時間の設定 msec 20 final long countNumber = 60000; 21 22 23 // インターバル msec 24 long interval = 100; 25 26 // レイアウトと関連 27 Button startButton = findViewById(R.id.start_button); 28 Button stopButton = findViewById(R.id.stop_button); 29 Button resetButton = findViewById(R.id.reset_button); 30 Button setButton = findViewById(R.id.set_button); 31 Button button1 = (Button) findViewById(R.id.main_button); 32 33 //テキストに初期時間の設定msec 34 timerText = findViewById(R.id.timer); 35 timerText.setText(dataFormat.format(180000)); 36 37 // インスタンス生成 38 // CountDownTimer(long millisInFuture, long countDownInterval) 39 final CountDown countDown = new CountDown(countNumber, interval); 40 41 //タイマー開始 42 startButton.setOnClickListener(new View.OnClickListener() { 43 @Override 44 public void onClick(View v) { 45 46 47 countDown.start(); 48 } 49 }); 50 51 //タイマー中止 52 stopButton.setOnClickListener(new View.OnClickListener(){ 53 @Override 54 public void onClick(View v) { 55 // 中止 56 countDown.cancel(); 57 } 58 }); 59 60 //タイマーリセット 61 resetButton.setOnClickListener(new View.OnClickListener(){ 62 @Override 63 public void onClick(View v) { 64 // リセット 65 timerText.setText(dataFormat.format(180000)); 66 } 67 }); 68 //タイマーに時間をセットする。 69 setButton.setOnClickListener(new View.OnClickListener() { 70 @Override 71 public void onClick(View v) { 72 //時間をセットする。 73 String time = edit.getText().toString(); 74 timerText.setText(time); 75 long time1 = Long.valueOf(time); 76 //エラー→ countNumber = timesec1 77 78 SharedPreferences data = getSharedPreferences("save", Context.MODE_PRIVATE); 79 SharedPreferences.Editor editor = data.edit(); 80 editor.putLong( "save", timesec1 ); 81 editor.commit(); 82 83/CountDownTimerクラス 84// class CountDown extends CountDownTimer { 85// 86// CountDown(long millisInFuture, long countDownInterval) { 87// super(millisInFuture, countDownInterval); 88// } 89// //タイマー終了後 90// @Override 91// public void onFinish() { 92// // 完了 93// timerText.setText(dataFormat.format(0)); 94// } 95// 96// // インターバルで呼ばれる 97// @Override 98// public void onTick(long millisInFuture) { 99// timerText.setText(dataFormat.format(millisInFuture)); 100// 101// } 102// }
java
試したこと
finalを書いてみましたが駄目でした。
Sharedpreferenceを使って時間を保存しようとしてみましたが反映されませんでした。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。