简体   繁体   English

我的代码没有正确更改TextView

[英]My code doesn't properly change the TextView

Everything works with no errors, but in the xml, tvfinalgrade stays 0.0... Why isn't the double fin being displayed? 一切正常,没有错误,但是在xml中,tvfinalgrade保持为0.0 ...为什么不显示双鳍? I'm sure about what order the tvfin code lines should be written, but I'm assuming its not right. 我确定应该写入tvfin代码行的顺序,但是我假设它不正确。

public class GFActivity extends Activity {
public double fin;
 @Override
    public void onCreate(Bundle savedInstanceState) {          

        super.onCreate(savedInstanceState);    
        setContentView(R.layout.getfinal);

        double q1, q2, ex;
         EditText etq1, etq2, eteg;
         etq1 = (EditText)findViewById(R.id.editText1);
            try{
                q1 = Double.parseDouble(etq1.getText().toString());
            } catch (NumberFormatException e) {
                q1=0;
            }
         etq2 = (EditText)findViewById(R.id.editText2);
            try{
                q2 = Double.parseDouble(etq2.getText().toString());
            } catch (NumberFormatException e){
                q2 = 0;
            }
         eteg = (EditText)findViewById(R.id.editText3);
         try{
             ex = Double.parseDouble(eteg.getText().toString());
         } catch (NumberFormatException e){
             ex = 0;
         }
        fin = 0.4*q1+0.4*q2+0.2*ex;
            if(fin == (int)fin){
                System.out.println((int)fin);
            }
            else{
                fin = 0.01*((int)(fin*100));
                System.out.println(fin);
            } 
            Button solve = (Button)findViewById(R.id.getfinbutton);
            solve.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    TextView tvfin= TextView)findViewById(R.id.tvfinalgrade);
                         tvfin.setText(fin+"");
                }
            });


    }
}

When you start the app, in the onCreate() you already parse the EditText content(which are empty, so you throw NumberFormatException and your fin variable ends up as 0 ) and when you get to the part of setting the result(the user clicks the Button ) you set the fin which is currently 0 to the TextView (the problem is that when the user clicks the Button you never get the current EditText content to do any calculation and the fin variable is the old value( 0 )). 启动应用程序时,在onCreate()您已经解析了EditText内容(它们为空,因此引发了NumberFormatException并且fin变量最终为0 ),然后进入设置结果的部分(用户单击Button ),将当前为0fin设置为TextView (问题是,当用户单击Button您永远不会获得当前的 EditText内容来进行任何计算,并且fin变量为旧值( 0 ))。 Move your calculation in the onClick() method: onClick()方法中移动您的计算:

Button solve = (Button)findViewById(R.id.getfinbutton);
            solve.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                   double q1, q2, ex;
         EditText etq1, etq2, eteg;
         etq1 = (EditText)findViewById(R.id.editText1);
            try{
                q1 = Double.parseDouble(etq1.getText().toString());
            } catch (NumberFormatException e) {
                q1=0;
            }
         etq2 = (EditText)findViewById(R.id.editText2);
            try{
                q2 = Double.parseDouble(etq2.getText().toString());
            } catch (NumberFormatException e){
                q2 = 0;
            }
         eteg = (EditText)findViewById(R.id.editText3);
         try{
             ex = Double.parseDouble(eteg.getText().toString());
         } catch (NumberFormatException e){
             ex = 0;
         }
        fin = 0.4*q1+0.4*q2+0.2*ex;
            if(fin == (int)fin){
                System.out.println((int)fin);
            }
            else{
                fin = 0.01*((int)(fin*100));
                System.out.println(fin);
            } 
                    TextView tvfin= TextView)findViewById(R.id.tvfinalgrade);
                         tvfin.setText(fin+"");
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM