Android Button没有做任何事情

时间:2016-06-30 17:14:18

标签: android button

我正在制作BMR(基础代谢率)的计算器,当我按下“计算(btnCal)”按钮时,它不会将结果写入TextView,即“维护”。我尝试修复它,但我不能使它工作。

这是我的代码。

package com.example.leks.calmeter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;


public class actCal extends AppCompatActivity implements View.OnClickListener
{
    private double act_val;
    public double mBMR, fBMR, BMR = 0;
    public EditText height, weight, age;
    TextView maintain;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_cal);

        init();


    }
    public void init()
    {
        Button btnCal;
        btnCal = (Button) findViewById(R.id.btnCal);
        btnCal.setOnClickListener(this);
    }

    @Override
    public void onClick(View view)
    {


        Spinner spinner2 = (Spinner)findViewById(R.id.spinner2);
        switch(view.getId())
        {
            case R.id.btnCal:
            {
                Spinner spinner = (Spinner)findViewById(R.id.spinner);
                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
                    {
                        String SelectedItem = parent.getItemAtPosition(position).toString();
                        if (SelectedItem.equals("Sedentary"))
                        {
                            act_val = 1.2;
                        }
                        if (SelectedItem.equals("Light"))
                        {
                            act_val = 1.375;
                        }
                        if (SelectedItem.equals("Moderate"))
                        {
                            act_val = 1.55;
                        }
                        if (SelectedItem.equals("Very Active"))
                        {
                            act_val = 1.725;
                        }
                        if (SelectedItem.equals("Extra Active"))
                        {
                            act_val = 1.9;
                        }
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });
                spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
                    {
                        String SelectedItem = parent.getItemAtPosition(position).toString();
                        if (SelectedItem.equals("Male"))
                        {
                            String heights = height.getText().toString();
                            String ages = age.getText().toString();
                            String weights = weight.getText().toString();
                            double wght = Double.parseDouble(weights);
                            double hght = Double.parseDouble(heights);
                            int ageint = Integer.parseInt(ages);
                            mBMR = ((10 * wght) + (6.25 * hght) - (5 * ageint) + 5) * act_val;
                            maintain.setText(String.valueOf(mBMR));

                        }
                        if (SelectedItem.equals("Female"))
                        {
                            //TODO Formula For Females here.
                        }

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });

            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

从我看到的,你没有正确设置你的onClick回调。单击按钮时所做的就是设置一个微调器。

为了在单击按钮时将文本写入TextView,您应该将init和onClick方法更改为以下内容:

public void init()
{
    Button btnCal;
    btnCal = (Button) findViewById(R.id.btnCal);
    btnCal.setOnClickListener(this);

    // Getting a reference to the text view - change the id to match what you have in your layout file
    maintain = (TextView) findViewById(R.id.maintain)
}

@Override
public void onClick(View view)
{


    Spinner spinner2 = (Spinner)findViewById(R.id.spinner2);
    switch(view.getId())
    {
        case R.id.btnCal:
            // Setting the text - change to whatever text you want
            maintain.setText("some text");

            // ...
            break;

    }
}

希望它有所帮助!