0

字符串.xml

  <string-array name="fruits">
       <item>Apple</item>
       <item>Banana</item>
       <item>Orange</item>
       <item>Pear</item>
       <item>Watermelon</item>
       <item>Mango</item>
       <item>Pineapple</item>
       <item>Strawberry</item>
</string-array>

<string-array name="total">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
</string-array>

<string-array name="calorie">
    <item>80</item>
    <item>101</item>
    <item>71</item>
    <item>100</item>
    <item>45</item>
    <item>135</item>
    <item>80</item>
    <item>53</item>
</string-array>    

Java 文件:

    public class Fruit extends Activity implements OnClickListener, OnItemSelectedListener {
private TextView tvFruit, tvNo;
Context context=this;   
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fruit);

    tvFruit = (TextView) findViewById(R.id.tvfruit);
    tvNo = (TextView) findViewById(R.id.tvno);


    final Spinner fruits = (Spinner)findViewById(R.id.spin_fruit);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this,R.array.fruits,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    fruits.setAdapter(adapter);
    fruits.setOnItemSelectedListener(this);

    fruits.setOnItemSelectedListener(new OnItemSelectedListener(){
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
            // TODO Auto-generated method stub
            String selectedItem = fruits.getSelectedItem().toString();
            tvFruit.setText(selectedItem);
        }

    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

    });

    final Spinner num = (Spinner)findViewById(R.id.spin_no);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
            this,R.array.total,android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    num.setAdapter(adapter1);
    num.setOnItemSelectedListener(this);

    num.setOnItemSelectedListener(new OnItemSelectedListener(){
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
                // TODO Auto-generated method stub
                String selectedItem = num.getSelectedItem().toString();
                tvNo.setText(selectedItem);
            }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

        });

    Button save = (Button) findViewById(R.id.bsave);
    save.setTextColor(Color.BLUE);
    save.setOnClickListener(new View.OnClickListener() {


            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                // Get the subject details and show it in an alertdialog
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setMessage("Success");
                builder.setPositiveButton("OK", null);
                builder.setNegativeButton("View Log", new DialogInterface.OnClickListener(){

                    public void onClick(DialogInterface dialog, int which) { // this part is done for the negative button 
                                                                             // if we want it to link to new intent
                        launchIntent();
                    }

                });
                builder.create().show();
            }

            //making the "View Log" button in dialog box to go to new intent FruitLog.class
            private void launchIntent(){
                Intent i = new Intent(Fruit.this, FruitLog.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }


        });

    }

因此,当用户在微调器中单击 3 个香蕉时,我想知道如何执行卡路里相乘的代码。 一旦完成计算,它应该显示 303 卡路里在

因此,当用户在微调器中单击 3 个香蕉时,我想知道如何执行卡路里相乘的代码。一旦完成计算,它应该在“总卡路里”文本视图中显示 303 卡路里

有人可以指导我如何进行计算编码。例如,帮我摄入 2 个苹果卡路里,剩下的我会试着弄清楚。非常感谢。这个社区非常有帮助。

4

1 回答 1

1

使用您现有的代码,我会尝试这样的事情:

首先,将您的calorie数组更改为integer-array. 接下来,添加:

public int calculateCalories() {
    int[] calorie = getResources().getIntArray(R.array.calorie);
    return Integer.parseInt((String) num.getSelectedItem()) * calorie[fruits.getSelectedItemPosition()];
}

此函数应返回所选水果的数量乘以它的热量值。明确地说,您将其直接设置为 TextView ,如下所示:

totalTextView.setText(String.valueOf(calculateCalories()));

您必须通过以numfruits声明相同的方式声明它们来使整个类可见tvFruit

于 2012-06-05T03:31:00.323 回答