如何使用Float数据类型调用setText()?

时间:2012-01-18 09:30:24

标签: android

    public void onClick(View v) {
    switch(v.getId()){
    case R.id.save2:
        s = text.getText().toString();
        number = Float.parseFloat(num.getText().toString());
        SharedPreferences.Editor editor = shared.edit();
        editor.putString("sharedtext",s);
        editor.putFloat("sharednum",number);
        editor.commit();
        Toast.makeText(this,"save2",Toast.LENGTH_SHORT).show();
        break;
    case R.id.load2:
        String returner = shared.getString("sharedtext","Returner fail");
        float returnnum = shared.getFloat("sharednum",0);
        text.setText(returner);
        num.setText(returnnum); //error here
        Toast.makeText(this,"load2",Toast.LENGTH_SHORT).show();
        break;
    case R.id.page2:
        intent= new Intent(this,SharedPreferencesActivity.class);
        startActivity(intent);
        Toast.makeText(this,"page2",Toast.LENGTH_SHORT).show();
        break;
    }

我怎么能解决这个错误?

如何制作类似setInt(int num);

的内容

btw变量num和text都是EditText

1 个答案:

答案 0 :(得分:9)

如果您将数值作为文本传递给文本字段,Android会尝试将其解释为资源ID。先把它作为文本。首选方法是:

num.setText(String.valueOf(returnnum));

(有关转换为字符串的良好做法,check this post