Setting string content
I'm trying to make a timer in which you can insert how much time you want by clicking on the textview or some other button. My problem is I can not figure out how to get the code right so that it will set the string. The string is one that is set up before the class in the string xml. Here is the code that I am working with. This is the dialog that shows when you want to set the string. Thanks. The length2 is the string that is erroring.
timeDisplay = (TextView) findViewById(R.id.timer);
timeDisplay.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View view) {
Dialog dialog = new Dialog(Score.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Set Time");
dialog.setCancelable(true);
final EditText min = (EditText) dialog.findViewById(R.id.min);
final EditText sec = (EditText) dialog.findViewById(R.id.sec);
Button btn = (Button)dialog.findViewById(R.id.positive);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Double min2 = Double.parseDouble(min.getText().toString());
Double sec2 = Double.parseDouble(sec.getText().toString());
{Double time = ((min2 * 60) + sec2);
length2.setText(time.toString());
}
}});
dialog.show();}
});
my string xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Score!</string>
<string name="app_name">Basketball Score Keeper</string>
<string name="start">Start</string>
<string name="pause">Pause</string>
<string name="resume">Resume</string>
<string name="restart">Restart</string>
<string name="timer">00:00</string>
<string name="length2">10000</string>
</resources>
my full code
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class Score extends Activity implements AdapterView.OnItemSelectedListener {
private Button Button01;
private Button Button02;
private Button Button03;
private Button Button04;
private TextView TextView01;
private TextView TextView02;
private TextView TextView03;
private TextView TextView06;
private Spinner Spinner01;
private Spinner Spinner02;
TextView timeDisplay;
int state = 0;
int length = R.string.length2;
long startTime = 0;
long currentTime = 0;
long timeElapsed = 0;
long timeRemaining = 0;
long prevTimeRemaining = 0;
Button control;
MyCount counter;
String[] goal={"Three", "Foul", "Two"};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button01 = (Button) findViewById(R.id.Button01);
Button02 = (Button) findViewById(R.id.Button02);
Button03 = (Button) findViewById(R.id.Button03);
Button04 = (Button) findViewById(R.id.Button04);
TextView01 = (TextView) findViewById(R.id.TextView01);
TextView02 = (TextView) findViewById(R.id.TextView02);
TextView03 = (TextView) findViewById(R.id.TextView03);
TextView06 = (TextView) findViewById(R.id.TextView06);
Spinner spin = (Spinner) findViewById(R.id.Spinner01);
spin.setOnItemSelectedListener((OnItemSelectedListener) this);
ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, goal);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
Button01.setOnLongClickListener(s开发者_如何学JAVAubtractLeft);
Button02.setOnClickListener(new Button.OnClickListener() {public void onClick(View v){my_func1();}});
Button03.setOnLongClickListener(subtractRight);
Button04.setOnClickListener(new Button.OnClickListener() {public void onClick(View v){my_func2();}});
timeDisplay = (TextView) findViewById(R.id.timer);
timeDisplay.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View view) {
Dialog dialog = new Dialog(Score.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Set Time");
dialog.setCancelable(true);
final EditText min = (EditText) dialog.findViewById(R.id.min);
final EditText sec = (EditText) dialog.findViewById(R.id.sec);
Button btn = (Button)dialog.findViewById(R.id.positive);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Double min2 = Double.parseDouble(min.getText().toString());
Double sec2 = Double.parseDouble(sec.getText().toString());
{Double time = ((min2 * 60) + sec2);
length2.setText(time.toString());
}
}});
dialog.show();}
});
control = (Button) findViewById(R.id.control);
counter = new MyCount(length, 100);
}
public void control(View view) {
switch (state) {
case 0:
startTime = System.currentTimeMillis();
counter.start();
control.setText(R.string.pause);
state = 1;
break;
case 1:
// pause
currentTime = System.currentTimeMillis();
timeElapsed = currentTime - startTime;
if (prevTimeRemaining == 0)
timeRemaining = length - timeElapsed;
else
timeRemaining = prevTimeRemaining - timeElapsed;
counter.cancel();
timeDisplay.setText(formatTime(timeRemaining));
control.setText(R.string.resume);
prevTimeRemaining = timeRemaining;
// resume
counter = new MyCount(timeRemaining, 100);
state = 0;
break;
case 2:
prevTimeRemaining = 0;
counter = new MyCount(length, 100);
control.setText(R.string.start);
timeDisplay.setText(R.string.timer);
state = 0;
}
}
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
public void onFinish() {
timeDisplay.setText("done!");
state = 2;
control.setText(R.string.restart);
}
public void onTick(long millisUntilFinished) {
timeDisplay.setText(formatTime(millisUntilFinished));
}
}
public String formatTime(long millis) {
String output = "00:00";
long seconds = millis / 1000;
long minutes = seconds / 60;
// long hours = minutes / 60;
seconds = seconds % 60;
minutes = minutes % 60;
// hours = hours % 60;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
// String hoursD = String.valueOf(hours);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
// if (hours < 10)
// hoursD = "0" + hours;
output = minutesD + " : " + secondsD;
return output;
}
public void setSpinner02(Spinner spinner02) {
Spinner02 = spinner02;
}
public Spinner getSpinner02() {
return Spinner02;
}
public void setSpinner01(Spinner spinner01) {
Spinner01 = spinner01;
}
public Spinner getSpinner01() {
return Spinner01;
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
{
if ("Three".equals(goal[position]))
{TextView03.setText("3");}
if ("Foul".equals(goal[position]))
{TextView03.setText("1");}
if ("Two".equals(goal[position]))
{TextView03.setText("2");}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
TextView03.setText("2");
TextView06.setText("2");
}
void my_func1() {
int scoreLeft;
int addOne;
int scoreLeftTotal;
addOne = Integer.parseInt(TextView03.getText().toString());
scoreLeft = Integer.parseInt(TextView01.getText().toString());
scoreLeftTotal = (scoreLeft + addOne);
TextView01.setText("" + scoreLeftTotal);
}
void my_func2() {
int scoreRight = Integer.parseInt(TextView02.getText().toString());
int addOne = Integer.parseInt(TextView03.getText().toString());
{int scoreRightTotal = (scoreRight + addOne);
TextView02.setText("" + scoreRightTotal);
}
}
private OnLongClickListener subtractLeft = new OnLongClickListener() {
public boolean onLongClick(View v) {
int scoreLeft = Integer.parseInt(TextView01.getText().toString());
int addOne = Integer.parseInt(TextView03.getText().toString());
{int scoreLeftTotal = (scoreLeft - addOne);
TextView01.setText("" + scoreLeftTotal);
}
return true;
}
};
private OnLongClickListener subtractRight = new OnLongClickListener() {
public boolean onLongClick(View v) {
int scoreRight = Integer.parseInt(TextView02.getText().toString());
int addOne = Integer.parseInt(TextView03.getText().toString());
{int scoreLeftTotal = (scoreRight - addOne);
TextView02.setText("" + scoreLeftTotal);
}
return true;
}
};
}
This is what I see as the problem:
length2
is not defined as a field in your code, you are copying the reference ID for the contents of what you have defined in the strings.xml
into the int length
variable. You never define the field length2
. I found this when I put your code into Eclipse.
I'm not sure what your are trying to do with this, but my educated guess is this: You are trying to capture the user's entered time desired for countdown to display somewhere (maybe in TextView timeDisplay
); because of your use of length2.setText()
.
Also, the statement: int length = R.string.length2;
will copy the hex ID for the string, not the string itself. So if you have in your strings.xml file: <string name="lenght2">100</string>
and you want that number stored into length
, then try changing that statement into:
String s_length = getString(R.string.length2);
int length = Integer.parseInt(s_length);
Hopefully this helps you in the right direction for what you are trying to accomplish.
Steve
精彩评论