2015-01-09 69 views
0

最近我开始捣乱Android Studio并决定制作一个应用程序。我已经充分利用了我的应用程序,但遇到了一些问题。我需要记住用户输入的一个变量,但我不知道如何去做,我尝试过在互联网上找到的解决方案,即使在这里,但我得到一个错误。 有人可以帮我一些想法或我必须在我的应用程序中编辑的代码? 这是该活动的Java代码:如何保存用户输入的号码?

public class calc_medie_teza extends ActionBarActivity implements View.OnClickListener { 
EditText adaug_nota; 
static final int READ_BLOCK_SIZE = 100; 
TextView afisare; 
TextView afisare2; 
Button calc; 
EditText note_nr; 
EditText nota_teza; 
int suma; 
double medie; 
double medieteza; 
int nr_note = 0; 
int notamedie; 
int notateza; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_calc_medie_teza); 
    afisare = (TextView) findViewById(R.id.afisare); 
    afisare2 = (TextView) findViewById(R.id.afisare2); 
    calc = (Button) findViewById(R.id.calc); 
    calc.setOnClickListener(this); 
    note_nr = (EditText) findViewById(R.id.note_nr); 
    nota_teza = (EditText) findViewById(R.id.nota_teza); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_calc_medie_teza, menu); // creare meniu 
    return true; 
} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); // meniu 
    return id == R.id.action_settings || super.onOptionsItemSelected(item); 
} 
public void onClick(View v) // afisare medie 
{ 
    calcul_medie(); 
} 

public void buton_adauga(View v) 
{ 
    if(note_nr ==) 
    suma = suma + notamedie; 
    nr_note = nr_note + 1; 
} 
public double calcul_medie() // calculul mediei 
{ 
    medie = suma/nr_note; 
    medieteza = ((medie * 3)+ notateza)/4; 
    return medieteza; 
} 

这里是与所述活性的照片:http://onlypro.ro/img/images/ootllv2f55hnwdgi0xlv.png

基本上应用需要输入号码添加到变量时我按下“Adauga NOTA” [添加等级]按钮,然后我必须插入“teza”号码,当按下“Calculeaza media”[Calculate]时,应用程序将计算calcul_medie()并返回一个十进制数。在“Introduceti notele aici”[在此添加成绩]和“Adaugata nota”[添加成绩]之间,我有一个数字输入文本,同样介于“Introduceti teza aici”[Add thesis bere]和“Calculeaza media”[Calculate]之间。我不知道如何存储用户放入的数字,并将其总结为“notamedie”变量。

我希望你能理解我的问题。 如果您有任何问题,我会尽快回复。

提前致谢!

+0

是指您在需要的号码,我用“字符串”变量EditText上? – kelvincer 2015-01-09 20:36:54

+0

@kelvincer是的,我需要编辑文本中的数字。 – R3muSGFX 2015-01-09 21:16:49

回答

2

好吧,我想你大概问如何从UI中的字符串输入并将其转化为整数/ BigDecimal的。如果是这样,这里是一些解决方案:

  1. 首先,你得从UI字符串:。

    字符串输入= note_nr.getText()的toString()修剪();

  2. 变化的字符串输入到整数/ BigDecimal的:

    INT NUMBER1 =的Integer.parseInt(输入);

    BigDecimal number2 = new BigDecimal(input);

纠正我,如果误解了你的问题。谢谢。

0

当你按下一个按钮,只是在检索按以下方式

edittext = (EditText)layout.findViewById(R.id.txtDescription); 
String string = edittext.getText().toString(); 
+0

我已经试过,但它不工作... – R3muSGFX 2015-01-09 21:20:06

+0

“不要开心”有一个很大的范围。你能解释究竟是什么问题? – Rohit5k2 2015-01-09 21:24:38

1

我听不懂的问题都从EditText上适当的值。请清楚定义问题并尽可能使用英语。正如我所了解的那样,将会有一个用户将输入该值的Edittext。获取它在活动中的价值,然后使用它。

EditText edittext; 
editext = (EditText) findViewById(R.id.editext); 
String value = edit text.getText().toString(); // the value that the user inputted in String data format 
// Here for addition you will need the value in int or decimal for that 
int valueInt = Integer.parse(value); // this will be the value in int type 
double valueInt = = Double.parseDouble(value); // this will be the value (user inputted) in double data type (which you will need if you want to use decimal numbers). 
+0

我很抱歉,但我用我的母语是罗马尼亚语的声明。 – R3muSGFX 2015-01-09 21:21:22

0

来检索文本做到这一点

String string = note_nr.getText().toString(); 

// Converting String to int 
int myValue =Integer.parseInt(string); 
0

我试了一次,但IDE说我用于“字符串”的变量从来没有使用过,但我用过它。

number1= (EditText) findViewById(R.id.number1); 
    String number_1 = number1.getText().toString(); 
number2= (EditText) findViewById(R.id.number2); 
String number_2= number2.getText().toString(); 
0

R3muSGFX,你能发表整个代码吗?

String number_1 = number1.getText()。toString();这行代码仅仅意味着你初始化了一个String变量“number_1”。

,你会用它当应用程序执行一个数学函数,就像你在开始的时候写道:

公共双calcul_medie(){ medie = SUMA/nr_note; medieteza =((medie * 3)+ notateza)/ 4; return medieteza; }

,但如果你没有在所有使用“数字_1”,IDE将意味着你的警告消息 :从未使用过