在 textView 上显示文本
Display text on textView
如何在textview上显示文字?当我点击 TimePicker
中的 ok
时,它崩溃了。
WorkDetails.java
public class WorkDetails extends AppCompatActivity implements View.OnClickListener {
private static EditText start;
private static EditText end;
private static TextView editText;
private TimePick tp;
private static int remains;
private static int startTime,endTime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workdetails);
start = (EditText) findViewById(R.id.editText6);
end = (EditText) findViewById(R.id.editText7);
editText=(TextView)findViewById(R.id.textView10);
tp = new TimePick();
start.setOnClickListener(this);
end.setOnClickListener(this);
addItemsOnSpinner();
}
public void onClick(View v) {
int id = v.getId();
if (id == R.id.editText6) {
tp.setFlag(TimePick.FLAG_START_DATE);
FragmentTransaction ft = getFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
} else if (id == R.id.editText7) {
tp.setFlag(TimePick.FLAG_END_DATE);
FragmentTransaction ft = getFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
}
}
public static class TimePick extends android.app.DialogFragment implements TimePickerDialog.OnTimeSetListener {
// EditText time;
public static final int FLAG_START_DATE = 0;
public static final int FLAG_END_DATE = 1;
private int flag = 0;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Calendar c= Calendar.getInstance();
int hour=c.get(Calendar.HOUR_OF_DAY);
int minute=c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(),this,hour,minute, DateFormat.is24HourFormat(getActivity()));
}
public void setFlag(int i) {
flag = i;
}
@Override
public void onTimeSet(TimePicker view,int hourofDay, int minute)
{
if (flag == FLAG_START_DATE) {
start.setText(Integer.toString(hourofDay) + "." + Integer.toString(minute));
startTime=hourofDay;
} else if (flag == FLAG_END_DATE) {
end.setText(Integer.toString(hourofDay) + "." + Integer.toString(minute));
endTime=hourofDay;
}
remains=endTime-startTime;
int a=remains;
editText.setText(a);
// Toast.makeText(getActivity(),"Time: "+remains,Toast.LENGTH_SHORT).show();
}
LogCat
Process: com.example.project.project, PID: 3718
android.content.res.Resources$NotFoundException: String resource ID #0xfffffff7
at android.content.res.Resources.getText(Resources.java:312)
at android.widget.TextView.setText(TextView.java:4417)
at com.example.project.project.WorkDetails$TimePick.onTimeSet(WorkDetails.java:106)
at android.app.TimePickerDialog.onClick(TimePickerDialog.java:145)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
在你的'onTimeSet()'方法中,你写
int a=remains;
editText.setText(a);
'setText()' 方法需要一个字符串。如果将它与 int 变量一起使用,该变量将被解释为来自 xml 文件 'res/values/strings.xml'.
的字符串资源的唯一 'R.string' 标识符
因此您的代码可以编译,但在大多数情况下,应用程序会崩溃。
要在 EditText 中显示 'a' 的值,请将您的代码更改为
int a=remains;
editText.setText("" + a);
如何在textview上显示文字?当我点击 TimePicker
中的 ok
时,它崩溃了。
WorkDetails.java
public class WorkDetails extends AppCompatActivity implements View.OnClickListener {
private static EditText start;
private static EditText end;
private static TextView editText;
private TimePick tp;
private static int remains;
private static int startTime,endTime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workdetails);
start = (EditText) findViewById(R.id.editText6);
end = (EditText) findViewById(R.id.editText7);
editText=(TextView)findViewById(R.id.textView10);
tp = new TimePick();
start.setOnClickListener(this);
end.setOnClickListener(this);
addItemsOnSpinner();
}
public void onClick(View v) {
int id = v.getId();
if (id == R.id.editText6) {
tp.setFlag(TimePick.FLAG_START_DATE);
FragmentTransaction ft = getFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
} else if (id == R.id.editText7) {
tp.setFlag(TimePick.FLAG_END_DATE);
FragmentTransaction ft = getFragmentManager().beginTransaction();
tp.show(ft, "TimePicker");
}
}
public static class TimePick extends android.app.DialogFragment implements TimePickerDialog.OnTimeSetListener {
// EditText time;
public static final int FLAG_START_DATE = 0;
public static final int FLAG_END_DATE = 1;
private int flag = 0;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Calendar c= Calendar.getInstance();
int hour=c.get(Calendar.HOUR_OF_DAY);
int minute=c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(),this,hour,minute, DateFormat.is24HourFormat(getActivity()));
}
public void setFlag(int i) {
flag = i;
}
@Override
public void onTimeSet(TimePicker view,int hourofDay, int minute)
{
if (flag == FLAG_START_DATE) {
start.setText(Integer.toString(hourofDay) + "." + Integer.toString(minute));
startTime=hourofDay;
} else if (flag == FLAG_END_DATE) {
end.setText(Integer.toString(hourofDay) + "." + Integer.toString(minute));
endTime=hourofDay;
}
remains=endTime-startTime;
int a=remains;
editText.setText(a);
// Toast.makeText(getActivity(),"Time: "+remains,Toast.LENGTH_SHORT).show();
}
LogCat
Process: com.example.project.project, PID: 3718
android.content.res.Resources$NotFoundException: String resource ID #0xfffffff7
at android.content.res.Resources.getText(Resources.java:312)
at android.widget.TextView.setText(TextView.java:4417)
at com.example.project.project.WorkDetails$TimePick.onTimeSet(WorkDetails.java:106)
at android.app.TimePickerDialog.onClick(TimePickerDialog.java:145)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
在你的'onTimeSet()'方法中,你写
int a=remains; editText.setText(a);
'setText()' 方法需要一个字符串。如果将它与 int 变量一起使用,该变量将被解释为来自 xml 文件 'res/values/strings.xml'.
的字符串资源的唯一 'R.string' 标识符因此您的代码可以编译,但在大多数情况下,应用程序会崩溃。
要在 EditText 中显示 'a' 的值,请将您的代码更改为
int a=remains;
editText.setText("" + a);