0

在此处输入图像描述

大家好,我的第一个 android 应用程序需要一些帮助,并且我有一点编程经验。所以首先我想做的是“让用户选择他们想要转换的任何方法,让他们在 TextView 中输入值,然后按下转换按钮以吐司结束它。”

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

    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();
        }
    };
}
4

2 回答 2

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());
        }
    }
}
于 2012-11-18T00:59:27.417 回答
0

尝试使用RadioGroup并设置RadioGroup.OnCheckedChangeListener

每次int用户checkedId更改RadioButton.

单击“转换”按钮时,只需使用块中RadioButton最后选择的 idswitch来决定使用哪个转换。

于 2012-11-18T01:15:36.900 回答