从TextView Android添加数字

时间:2015-03-04 17:48:02

标签: android textview add

我需要选择一个按钮,' H'它表示周期表中的原子元素,根据需要多次,并且必须将总重量数加起来,并将其显示在mw_results中。

到目前为止,我只能将它显示两次,因为我已经对它进行了硬编码。

任何想法如何我可以继续添加数字,而无需硬编码..?

感谢!

以下是代码:

public void Chem()
    {
        final Dialog g = new Dialog(Sol.this);
        g.setContentView(R.layout.table);

        final float[] MoWeight = {0};

        mw_result = (TextView)findViewById(R.id.editText);
        mf_result = (EditText)findViewById(R.id.editText4);
        Chemname = "";
        final String space = " ";
        final int number = 0;
        Button H = (Button) g.findViewById(R.id.H);
        Button C = (Button) g.findViewById(R.id.C);
        H.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                switch (v.getId()) {

                    case R.id.H:
                        mf_result.append(String.valueOf("H"));
                        mw_result.setText(String.valueOf(123));
                        float MoWeight = Float.valueOf(mw_result.getText().toString());
                        MoWeight = Float.valueOf(mw_result.getText().toString()) + MoWeight;
                        String mw_res=Float.toString(MoWeight);
                        mw_result.setText(mw_res);
                        mw_result = (TextView)findViewById(R.id.editText);
                        g.dismiss();

                }
            }
        });

 C.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                switch (v.getId()) {

                    case R.id.C:
                        mf_result.append(String.valueOf("C"));
                        mw_result.setText(String.valueOf(456));
                        float MoWeight = Float.valueOf(mw_result.getText().toString());
                        MoWeight = Float.valueOf(mw_result.getText().toString()) + MoWeight;
                        String mw_res=Float.toString(MoWeight);
                        mw_result.setText(mw_res);
                        mw_result = (TextView)findViewById(R.id.editText);
                        g.dismiss();
                }
            }
        });
        g.show();
    }

2 个答案:

答案 0 :(得分:0)

试试这个:

    public void Chem()
    {
    final Dialog g = new Dialog(Sol.this);
    g.setContentView(R.layout.table);

    final float[] MoWeight = {0};

    mw_result = (TextView)findViewById(R.id.editText);
    mf_result = (EditText)findViewById(R.id.editText4);
    Chemname = "";
    final String space = " ";
    final int number = 0;

    Button H = (Button) g.findViewById(R.id.H);
    Button C = (Button) g.findViewById(R.id.C);

    mw_result.setTag(1);//1 in case you're updating 123 values 
    mw_result.setTag(2);//2 in case you're updating 456 values 

    H.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            int key = Integer.parseInt(mw_result.getTag().toString());
            if(key==2){
                mw_result.setTag(1);//1 in case you're updating 123 values 
                mf_result.append(String.valueOf("H"));
                mw_result.setText(String.valueOf(123));                    
                g.dismiss();
            }
            else{
                float MoWeight = Float.valueOf(mw_result.getText().toString());
                MoWeight = Float.valueOf(mw_result.getText().toString()) + 1;
                String mw_res=Float.toString(MoWeight);
                mw_result.setText(mw_res);
            }
        }
    });

    C.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            int key = Integer.parseInt(mw_result.getTag().toString());
            if(key==1){
                mw_result.setTag(2);//1 in case you're updating 123 values 
                mf_result.append(String.valueOf("C"));
                mw_result.setText(String.valueOf(456));                 
                g.dismiss();
            }
            else{
                float MoWeight = Float.valueOf(mw_result.getText().toString());
                MoWeight = Float.valueOf(mw_result.getText().toString()) + 1;
                String mw_res=Float.toString(MoWeight);
                mw_result.setText(mw_res);
            }
        }
    });
    g.show();
}

答案 1 :(得分:0)

您似乎在每次点击时将文本视图分别重置为123和456.

很难确切地看到你想要的东西,但这可能会帮助你走上正轨:

    public void Chem()
    {
        final Dialog g = new Dialog(Sol.this);
        g.setContentView(R.layout.table);

        final float[] MoWeightArray = {0};  //rename this for readability -- is this used at all?

        mw_result = (TextView)findViewById(R.id.editText);
        mf_result = (EditText)findViewById(R.id.editText4);
        Chemname = "";
        final String space = " ";
        final int number = 0;
        Button H = (Button) g.findViewById(R.id.H);
        Button C = (Button) g.findViewById(R.id.C);
        H.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                switch (v.getId()) {

                    case R.id.H:
                      String sMW= mw_result.getText().toString();
                      mf_result.append(String.valueOf("H"));  
                      if (sMW.matches("")) { 
                        mw_result.setText(String.valueOf(123));
                      }
                      else{
                        float MoWeight = Float.valueOf(sMW);
                        MoWeight += 123;
                        String mw_res=Float.toString(MoWeight);
                        mw_result.setText(mw_res);
                        //mw_result = (TextView)findViewById(R.id.editText); no need for this 
                        g.dismiss();
                      }

                }
            }
        });

 C.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                switch (v.getId()) {

                    case R.id.C:
                      String sMW= mw_result.getText().toString();
                      mf_result.append(String.valueOf("C"));
                      if (sMW.matches("")) {
                        mw_result.setText(String.valueOf(456));
                      }
                      else{ 
                        float MoWeight = Float.valueOf(sMW);
                        MoWeight += 456;
                        String mw_res=Float.toString(MoWeight);
                        mw_result.setText(mw_res);
                        //mw_result = (TextView)findViewById(R.id.editText); No need for this
                        g.dismiss();
                      }
                }
            }
        });
        g.show();
    }