Android Radio按钮if还是切换?

时间:2012-11-18 00:47:19

标签: android radio-button switch-statement onclicklistener buttonclick

enter image description here

嘿大家,我需要一些帮助我的第一个Android应用程序,我有一点编程经验。所以我首先要做的就是"让用户选择他们想要转换的方法,然后让他们在TextView中输入值,然后按转换按钮完成烤面包。 "

目前我无法使用onClickListener()按钮完成toast输出,我无法从switch / case中的String获取结果。另外我不确定如果我的代码没问题,因为我无法测试它:(。我花了几个小时试图让它工作但没有运气。需要有人指出我正确的方向和我需要看的东西,请

    class convert_handler_CMtoM implements Button.OnClickListener {

    public void onClick(View v) {

        if (v == btnDisplay) {

            if (MMtoCM.isChecked())
            {
                int Amount = (int) Float.parseFloat(et1.getText().toString());
                int formula = (int) (Amount * 10) ; 
                String result = String.valueOf(formula);
            }

            if (CMtoMM.isChecked())
            {
                double Amount = (double)Float.parseFloat(et1.getText().toString());
                double formula = (double) (Amount / 10) ; 
                String result = String.valueOf(formula);
            }
        }

    }   

    private OnClickListener btnDisplay = new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            RadioButton rb = (RadioButton) v;
            Toast.makeText(second.this, rb.getText(), result, Toast.LENGTH_SHORT).show();
        }
    };
}

2 个答案:

答案 0 :(得分:2)

package com.example.radiobuttongroup;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.radiobuttongroup.R.id;

public class MainActivity extends Activity {

    RadioButton rd1, rd2, rd3;
    Button btn1;
    int type;
    String title;
    EditText input;
    AlertDialog.Builder editalert;
    AlertDialog alertd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rd1 = (RadioButton) findViewById(id.radioButton1);
        rd2 = (RadioButton) findViewById(id.radioButton2);
        rd3 = (RadioButton) findViewById(id.radioButton3);
        btn1 = (Button) findViewById(id.button1);



        rd1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                rd2.setChecked(false);
                rd3.setChecked(false);
                type = 1;
                title = "cm to m";
            }
        });

        rd2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                rd1.setChecked(false);
                rd3.setChecked(false);
                type = 2;
                title = "m to cm";

            }
        });

        rd3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                rd2.setChecked(false);
                rd1.setChecked(false);
                type = 3;
                title = "mm to cm";
            }
        });

        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                calculate(title, type);

            }
        });
    }

    void calculate(String title, final int type) {
        try {

            editalert = new AlertDialog.Builder(MainActivity.this);
            alertd = editalert.create();
            editalert.setTitle("Calculator");
            editalert.setMessage(title);
            input = new EditText(MainActivity.this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.FILL_PARENT);
            input.setLayoutParams(lp);
            input.setText("1000");
            editalert.setView(input);

            editalert.setPositiveButton("Convert",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            int value = Integer.valueOf(input.getText().toString());

                            switch (type) {
                            case 1:
                                Toast.makeText(
                                        MainActivity.this,
                                        String.valueOf(value / 100),
                                        Toast.LENGTH_LONG).show();
                                dialog.cancel();
                                alertd.dismiss();
                                break;

                            case 2:
                                Toast.makeText(MainActivity.this,
                                        String.valueOf(value * 100),
                                        Toast.LENGTH_LONG).show();
                                dialog.cancel();
                                alertd.dismiss();
                                break;

                            case 3:
                                Toast.makeText(MainActivity.this,
                                        String.valueOf(value / 100),
                                        Toast.LENGTH_LONG).show();
                                dialog.cancel();
                                alertd.dismiss();
                                break;
                            default:
                                break;
                            }

                        }
                    });
            editalert.show();
        } catch (Exception e) {
            Log.v("hata", e.toString());
        }
    }
}

答案 1 :(得分:0)

尝试使用RadioGroup并设置RadioGroup.OnCheckedChangeListener

使用全局int并在每次用户更改checkedId的选择时将其设置为侦听器的RadioButton参数。

点击“转换”按钮后,只需使用RadioButton块中最后选择的switch的ID来决定使用哪种转化。