【问题标题】:Have to tap 2 times in a button to have a result必须在按钮中点击 2 次才能获得结果
【发布时间】:2011-03-17 14:57:03
【问题描述】:

公共无效计算() { 最后的 EditText 卷; 最后的 EditText 杀; 最后的 EditText 缺点; 上下文 = getApplicationContext(); vol = (EditText) findViewById(R.id.volume2); kil= (EditText) findViewById(R.id.kilometrage2); cons= (EditText) findViewById(R.id.consom2);

 final Button button = (Button) findViewById(R.id.button);

 button.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {

 String value1 = vol.getText().toString();
 String value2 = kil.getText().toString();


                     if (value1 != null && /*value1.trim().length() > 0 &&*/ value2 != null /*&& value2.trim().length() > 0*/)
                     {
                             float q1=Float.parseFloat(vol.getText().toString());
                             float q2=Float.parseFloat(kil.getText().toString());
                             float x=((q1 / q2)* 100);
                             String y= Float.toString(x);
                             cons.setText(y);

                             SimpleDateFormat format = new SimpleDateFormat("dd/MM");
                              String date = format.format(new Date());
                              data = date + " : " + y + "L/100KM"+ " " + value1 + "L "+ value2 + "KM\n";
       SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
      if (data != "" ) {
      String fileName = getResources().getString(R.string.fileName);
      String fileDir = ""+ preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
      myIO.WriteSettings(context, fileDir + fileName, data);
      data = "";

      Toast.makeText(carburant.this, "Donnée ajoutée!", Toast.LENGTH_SHORT).show();
                                }

                     }
                     else 
                     {
                     Toast.makeText(carburant.this, "Veuillez vérifier les deux champs", Toast.LENGTH_SHORT).show();
                     }
     }

});

WriteSettings 方法:

public class myIO {
public static void WriteSettings(Context context, String nom, String data) {
    FileOutputStream fOut = null;
    OutputStreamWriter osw = null;

    try {
        fOut = context.openFileOutput(nom, Context.MODE_APPEND);
        osw = new OutputStreamWriter(fOut);
        osw.write(data);
        osw.flush();
        osw.close();
        fOut.close();

    } catch (Exception e) {
        Toast.makeText(context, "Echec", Toast.LENGTH_SHORT).show();
    } finally {
        try {
            osw.close();
            fOut.close();
        } catch (IOException e) {
            Toast.makeText(context, "Echec", Toast.LENGTH_SHORT).show();
        }
    }
}

【问题讨论】:

  • 您是否将写入刷新到文件中?
  • 是的,我将在第一篇文章中添加 WriteSettings 方法。

标签: android file-io button


【解决方案1】:

代码的sn-p是怎么调用的?什么是 value1/value2?如果这是点击事件,则 value1 或 value2 可能在第一次点击时为空值,从而阻止更改任何设置。

【讨论】:

  • 我认为它在一个监听器的 onClick 事件中,因为他有 });在底部,但他省略了顶部。
  • @DMags, @Tim:我用全功能编辑了我的第一篇文章!感谢您尝试帮助我:)。
  • 不客气 :) 你确定你应该在 GetText() 之后使用 toString() 函数来获取 value1/value2 吗?我相信这是为了获取类名,而不是为了转换为字符串类型(虽然我可能是错的,如果函数重载)。还有,vol 和 kil 的值是多少?
猜你喜欢
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
  • 2015-07-02
  • 1970-01-01
相关资源
最近更新 更多