我想计算sharedPreference中的变量。一些计算可能包括乘法和“if else”。例如,如果变量大于4,则乘以10,如果大于7则乘以11.
任何人都可以展示如何做到这一点的例子?
金钱是涉及乘法的。错误说运算符“*”不能应用于android.widget.EditText。如何解决这个问题?
package com.example.shyan.prototype;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class user_input2 extends Activity {
public static final String PREFS_NAME = "MyPreferenceFile";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_input2);
final EditText item = (EditText) findViewById(R.id.editTextpower);
final EditText price = (EditText) findViewById(R.id.editTextusage);
Button btn = (Button) findViewById(R.id.buttonsave);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("item", item.getText().toString());
editor.putString("price", price.getText().toString());
editor.putString("money", item*price);
editor.commit();
Intent intent = new Intent(getApplicationContext(), Report.class);
startActivity(intent);
}
});
}
}
答案 0 :(得分:0)
你的问题是你不能像这样多个editTexts,如果你想要数字值,那么这样做:
final EditText item_et = (EditText) findViewById(R.id.editTextpower);
final EditText price_et = (EditText) findViewById(R.id.editTextusage);
int item=Integer.parseInt(item.getText().toString());
int price = Integer.parseInt(price.getText().toString());
现在您可以执行item * price并将其保存为这样的int:
editor.puInt("money",item*price)