如何使用带有文本视图列表的单选按钮

时间:2015-11-16 17:32:46

标签: java android xml

我开发了一个Android应用程序,用户可以将用户所选单位的速度转换为其他单位。

界面如下: enter image description here

当用户输入一个值并选择一个单选按钮时,textview旁边的0应显示转换后的单位。

到目前为止我完成的Java代码如下:

package com.firstapplication.aditya.multicalc;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;



public class Speed_Converter extends Activity implements View.OnClickListener{

Typeface font;
EditText txtOne;
TextView valueOne, valueTwo, valueThree, valueFour, valueFive;
Button main;
RadioGroup radioButtonGroup;
RadioButton radioOne, radioTwo, radioThree, radioFour, radioFive;
double valOne, valTwo, valThree, valFour, valFive;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.speed_converter);

    font = Typeface.createFromAsset(getAssets(),"fonts/Aliquam.ttf");
    initialize();



    main.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Speed_Converter.this, Main.class);
            intent.putExtra("a", true);
            startActivity(intent);

        }
    });

    radioButtonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            int id = radioButtonGroup.getCheckedRadioButtonId();
            View radioButton = radioButtonGroup.findViewById(id);
            if(radioButton.getId() == R.id.radio1)
            {
                calculateOne();
            }
            if(radioButton.getId() == R.id.radio2)
            {
                calculateTwo();
            }
            if(radioButton.getId() == R.id.radio3)
            {
                calculateThree();
            }
            if(radioButton.getId() == R.id.radio4)
            {
                calculateFour();
            }
            if(radioButton.getId() == R.id.radio5)
            {
                calculateFive();
            }
        }
    });


}

private void initialize()
{

    main = (Button) findViewById(R.id.button38);

    txtOne = (EditText)findViewById(R.id.textView4);


    radioOne = (RadioButton) findViewById(R.id.radio1);
    radioTwo = (RadioButton) findViewById(R.id.radio2);
    radioThree = (RadioButton) findViewById(R.id.radio3);
    radioFour = (RadioButton) findViewById(R.id.radio4);
    radioFive = (RadioButton) findViewById(R.id.radio5);


    valueOne = (TextView)findViewById(R.id.textView22);
    valueTwo = (TextView)findViewById(R.id.textView28);
    valueThree = (TextView)findViewById(R.id.textView26);
    valueFour = (TextView)findViewById(R.id.textView29);
    valueFive = (TextView)findViewById(R.id.textView31);

    radioButtonGroup = (RadioGroup)findViewById(R.id.radioGroup1);


}


private void calculateOne()
{
    valOne = Double.parseDouble(txtOne.toString());
    valueOne.setText("" + valOne);

    valTwo = Double.parseDouble(txtOne.toString())*767.269;
    valueTwo.setText("" + valTwo);

    valThree = Double.parseDouble(txtOne.toString())*666.739;
    valueThree.setText("" + valThree);

    valFour = Double.parseDouble(txtOne.toString())*1234.8;
    valueFour.setText("" + valFour);

    valFive = Double.parseDouble(txtOne.toString())*343;
    valueFive.setText("" + valFive);
}

private void calculateTwo()
{
    valOne = Double.parseDouble(txtOne.toString())*1234.8;
    valueOne.setText("" + valOne);

    valTwo = (Double.parseDouble(txtOne.toString()));
    valueTwo.setText("" + valTwo);

    valThree = Double.parseDouble(txtOne.toString())*343;
    valueThree.setText("" + valThree);

    valFour = (Double.parseDouble(txtOne.toString()))*666.739;
    valueFour.setText("" + valFour);

    valFive = Double.parseDouble(txtOne.toString())*767.269;
    valueFive.setText("" + valFive);
}

private void calculateThree()
{
    valOne = Double.parseDouble(txtOne.toString())*3.6;
    valueOne.setText("" + valOne);

    valTwo = Double.parseDouble(txtOne.toString())*0.0029;
    valueTwo.setText("" + valTwo);

    valThree = Double.parseDouble(txtOne.toString());
    valueThree.setText("" + valThree);

    valFour = Double.parseDouble(txtOne.toString())*1.944;
    valueFour.setText("" + valFour);

    valFive = Double.parseDouble(txtOne.toString())*2.237;
    valueFive.setText("" + valFive);
}

private void calculateFour()
{
    valOne = Double.parseDouble(txtOne.toString());
    valueOne.setText("" + valOne);

    valTwo = Double.parseDouble(txtOne.toString())*0.0008;
    valueTwo.setText("" + valTwo);

    valThree = Double.parseDouble(txtOne.toString())*0.278;
    valueThree.setText("" + valThree);

    valFour = Double.parseDouble(txtOne.toString())*0.54;
    valueFour.setText("" + valFour);

    valFive = Double.parseDouble(txtOne.toString())*0.621;
    valueFive.setText("" + valFive);
}

private void calculateFive()
{
    valOne = Double.parseDouble(txtOne.toString())*1.609;
    valueOne.setText("" + valOne);

    valTwo = Double.parseDouble(txtOne.toString())*0.0013;
    valueTwo.setText("" + valTwo);

    valThree = Double.parseDouble(txtOne.toString())*0.447;
    valueThree.setText("" + valThree);

    valFour = Double.parseDouble(txtOne.toString())*0.869;
    valueFour.setText("" + valFour);

    valFive = Double.parseDouble(txtOne.toString());
    valueFive.setText("" + valFive);
}



@Override
public void onClick(View v) {

}


}

以上设计的XML如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical">

<Button
    android:id="@+id/btnHeader"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#0000"
    android:text="SPEED CONVERTER"
    android:textColor="#FFFFFF"
    android:textSize="20dp" />

<EditText
    android:id="@+id/textView4"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:inputType="numberDecimal"
    android:background="#FFFFFF"
    android:text=""/>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
   >



    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="top|left">

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id = "@+id/radioGroup1"
            >

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:layout_marginLeft="170dp"
            android:text="Mach"
            android:textColor="#FFFFFF"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Miles per Hour"
                android:textColor="#FFFFFF"
                android:id="@+id/radio2"
                android:layout_marginLeft="170dp"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Knot"
                android:textColor="#FFFFFF"
                android:id="@+id/radio3"
                android:layout_marginLeft="170dp"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Kilometers per Hour"
                android:textColor="#FFFFFF"
                android:id="@+id/radio4"
                android:layout_marginLeft="170dp"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Meters per Second"
                android:textColor="#FFFFFF"
                android:id="@+id/radio5"
                android:layout_marginLeft="170dp"/>

        </RadioGroup>

    </RelativeLayout>



    <Button
        android:id="@+id/button37"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#FFFFFF"
        android:layout_gravity="center_horizontal"
        android:text=""
        android:layout_above="@+id/button38"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <Button
        android:id="@+id/button38"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0000"
        android:layout_gravity="center_horizontal"
        android:text="Main Menu"
        android:textColor="#FFFFFF"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="20dp"
        android:gravity="right"
        android:textColor="#FFFFFF"
        android:id="@+id/textView22"
        android:layout_below="@+id/radioGroup"
        android:layout_alignLeft="@+id/textView26"
        android:layout_alignStart="@+id/textView26"
        android:layout_marginTop="33dp" />

    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Miles per Hour    "
        android:textSize="20dp"
        android:textColor="#FFFFFF"
        android:gravity="right"
        android:id="@+id/textView23"
        android:layout_above="@+id/textView25"
        android:layout_alignLeft="@+id/textView25"
        android:layout_alignStart="@+id/textView25" />


    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Knot    "
        android:textSize="20dp"
        android:gravity="right"
        android:textColor="#FFFFFF"
        android:id="@+id/textView25"
        android:layout_above="@+id/textView27"
        android:layout_alignLeft="@+id/textView27"
        android:layout_alignStart="@+id/textView27" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="20dp"
        android:textColor="#FFFFFF"
        android:id="@+id/textView26"
        android:layout_alignTop="@+id/textView25"
        android:layout_toRightOf="@+id/textView25"
        android:layout_toEndOf="@+id/textView25" />

    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Kilometers per Hour    "
        android:textSize="20dp"
        android:gravity="right"
        android:textColor="#FFFFFF"
        android:id="@+id/textView27"
        android:layout_above="@+id/textView30"
        android:layout_toLeftOf="@+id/textView31"
        android:layout_toStartOf="@+id/textView31" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="20dp"
        android:textColor="#FFFFFF"
        android:id="@+id/textView28"
        android:layout_below="@+id/textView22"
        android:layout_alignLeft="@+id/textView26"
        android:layout_alignStart="@+id/textView26" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="20dp"
        android:id="@+id/textView29"
        android:textColor="#FFFFFF"
        android:layout_below="@+id/textView25"
        android:layout_toRightOf="@+id/textView27"
        android:layout_toEndOf="@+id/textView27" />

    <TextView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Meters per Second    "
        android:textSize="20dp"
        android:gravity="right"
        android:id="@+id/textView30"
        android:textColor="#FFFFFF"
        android:layout_above="@+id/button37"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="42dp"
        android:layout_marginStart="42dp"
        android:layout_marginBottom="39dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="20dp"
        android:id="@+id/textView31"
        android:textColor="#FFFFFF"
        android:layout_alignBaseline="@+id/textView30"
        android:layout_alignBottom="@+id/textView30"
        android:layout_toRightOf="@+id/textView30"
        android:layout_toEndOf="@+id/textView30" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mach    "
        android:textSize="20dp"
        android:textColor="#FFFFFF"
        android:id="@+id/textView21"
        android:gravity="right"
        android:layout_above="@+id/textView23"
        android:layout_alignRight="@+id/textView23"
        android:layout_alignEnd="@+id/textView23" />


</RelativeLayout>

</LinearLayout>

但是,当我运行应用程序并在EditText中设置一个值并选择一个单选按钮时,应用程序停止。

任何帮助都将不胜感激。

logcat如下:

11-16 23:29:44.136  17088-17088/com.firstapplication.aditya.multicalc I/art﹕ Late-enabling -Xcheck:jni
11-16 23:29:44.346  17088-17108/com.firstapplication.aditya.multicalc D/OpenGLRenderer﹕ Render dirty regions requested: false
11-16 23:29:44.356  17088-17088/com.firstapplication.aditya.multicalc D/Atlas﹕ Validating map...
11-16 23:29:44.426  17088-17108/com.firstapplication.aditya.multicalc D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
11-16 23:29:44.431  17088-17108/com.firstapplication.aditya.multicalc D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
11-16 23:29:44.476  17088-17108/com.firstapplication.aditya.multicalc D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
11-16 23:29:44.526  17088-17108/com.firstapplication.aditya.multicalc E/﹕ Device driver API match
    Device driver API version: 23
    User space API version: 23
11-16 23:29:44.526  17088-17108/com.firstapplication.aditya.multicalc E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct  9 21:05:57 KST 2013
11-16 23:29:44.526  17088-17108/com.firstapplication.aditya.multicalc I/OpenGLRenderer﹕ Initialized EGL, version 1.4
11-16 23:29:44.531  17088-17108/com.firstapplication.aditya.multicalc D/OpenGLRenderer﹕ Enabling debug mode 0
11-16 23:29:44.816  17088-17088/com.firstapplication.aditya.multicalc I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@54dbb77 time:68917579
11-16 23:29:52.831  17088-17088/com.firstapplication.aditya.multicalc I/Timeline﹕ Timeline: Activity_launch_request id:com.firstapplication.aditya.multicalc time:68925592
11-16 23:29:52.996  17088-17103/com.firstapplication.aditya.multicalc I/art﹕ Background sticky concurrent mark sweep GC freed 8779(383KB) AllocSpace objects, 1(12KB) LOS objects, 6% free, 5MB/6MB, paused 5.518ms total 43.500ms
11-16 23:29:53.211  17088-17088/com.firstapplication.aditya.multicalc I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@331eff48 time:68925974
11-16 23:29:55.841  17088-17088/com.firstapplication.aditya.multicalc I/Timeline﹕ Timeline: Activity_launch_request id:com.firstapplication.aditya.multicalc time:68928601
11-16 23:29:56.341  17088-17088/com.firstapplication.aditya.multicalc I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@1cfb530a time:68929103
11-16 23:30:02.826  17088-17088/com.firstapplication.aditya.multicalc W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection

1 个答案:

答案 0 :(得分:1)

以下代码

valOne = Double.parseDouble(txtOne.toString());

不会像预期的那样工作,因为这样你试图解析EditText对象的字符串表示&#39; txtOne&#39;。相反,你必须写

valOne = Double.parseDouble(txtOne.getText().toString());

以获取用户输入。