androidstudio的java代码,用于获取用户输入和显示输出

dluptydi  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(293)

在这里,我尝试在文本视图中获取用户输入和显示输出,但是checkgen和checkuser返回-1,尽管选择了它们,而且两个单选按钮都被选中了。我想显示在文本视图中输入的所有详细信息。我也在下面粘贴了activity\u reg.xml代码
注册java

package com.example.bloodbank;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class reg extends AppCompatActivity {

    //login button
    Button btn_login;
    //firstname
    EditText fn;
    //lastname
    EditText ln;
    //phone number
    EditText pn;
    //output
    TextView op;
    //registration button
    Button btn_reg;
    //radio group for gender
    RadioGroup rggen;
    //radio group for user(donor/in search of blood)
    RadioGroup rguser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        btn_login=findViewById(R.id.btn_login);
        fn=findViewById(R.id.txtfirstname);
        ln=findViewById(R.id.txtlastname);
        pn=findViewById(R.id.txtmobile);
        op=findViewById(R.id.outputreg);
        rggen=findViewById(R.id.rggender);
        rguser=findViewById(R.id.rgreg);
        btn_reg=findViewById(R.id.main_btn_reg);

        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String fname=fn.getText().toString();
                String lname=ln.getText().toString();
                int pno= new Integer(pn.getText().toString());

                int checkgen=rggen.getCheckedRadioButtonId();
                //Toast.makeText(reg.this, "checkgen= "+checkgen, Toast.LENGTH_SHORT).show();
                int checkuser=rguser.getCheckedRadioButtonId();
                //Toast.makeText(reg.this, "checkuser= "+checkuser, Toast.LENGTH_SHORT).show();
                if(((checkgen == -1)||(checkuser == -1))||((checkgen == -1)&&(checkuser == -1)))
                {
                    //no radio button selected
                    Toast.makeText(reg.this, "Please select an option", Toast.LENGTH_SHORT).show();
                }
                else {
                    //radio button is checked
                  findRadioButton1(checkgen);
                  findRadioButton2(checkuser);
                   op.setText("Welcome " + fname + " " + lname + "Phone Number: " +pno );
                }

            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(reg.this,login.class);
                startActivity(i);

            }
        });
    }

    private void findRadioButton2(int checkuser) {
        switch(checkuser){
            case R.id.rbdonor:
                op.setText("\nUser: Donor\n");
                break;
            case R.id.rbneedy:
                op.setText("\nUser: In search of blood\n");
                break;

        }
    }

    private void findRadioButton1(int checkgen) {
        switch(checkgen){
            case R.id.rbmale:
                op.setText("\nGender: Male\n");
                break;
            case R.id.rbfemale:
                op.setText("\nGender: Female\n");
                break;

        }
    }
}

活动\u reg.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rbmale"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".reg">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:elevation="23dp"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txtregister"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Registration"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="20dp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="20dp"
                    android:orientation="vertical">

                    <LinearLayout

                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal"
                        android:weightSum="10"

                        >

                        <EditText
                            android:id="@+id/txtfirstname"
                            android:layout_width="match_parent"
                            android:layout_height="50dp"

                            android:hint="First Name*"
                            android:paddingLeft="8dp"
                            android:paddingTop="15dp"

                            android:singleLine="true"
                            android:textColor="@color/colorPrimaryDark"
                            android:textColorHint="@color/colorPrimaryDark"
                            android:textSize="13dp" />

                    </LinearLayout>

                    <LinearLayout

                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal"
                        android:weightSum="10"

                        >

                        <EditText
                            android:id="@+id/txtlastname"
                            android:layout_width="match_parent"
                            android:layout_height="50dp"

                            android:hint="Last Name*"
                            android:paddingLeft="8dp"
                            android:paddingTop="15dp"
                            android:singleLine="true"
                            android:textColor="@color/colorPrimaryDark"
                            android:textColorHint="@color/colorPrimaryDark"
                            android:textSize="13dp" />

                    </LinearLayout>

                    <LinearLayout

                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="3"

                        >

                        <TextView
                            android:id="@+id/textView"
                            android:layout_width="90dp"
                            android:layout_height="30dp"
                            android:layout_weight="1"
                            android:text="  Gender*:"
                            android:textColor="#A31D13" />

                        <RadioGroup
                            android:id="@+id/rggender"
                            android:layout_width="309dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1">

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:orientation="horizontal">

                                <RadioButton
                                    android:id="@+id/rbmale"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_weight="1"
                                    android:text="Male"
                                    android:textColor="#BE202F" />

                                <RadioButton
                                    android:id="@+id/rbfemale"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_weight="1"
                                    android:text="Female"
                                    android:textColor="#BE202F" />
                            </LinearLayout>
                        </RadioGroup>

                    </LinearLayout>

                    <LinearLayout

                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal"
                        android:weightSum="10"

                        >

                        <EditText
                            android:id="@+id/txtmobile"
                            android:layout_width="match_parent"
                            android:layout_height="50dp"
                            android:hint="Mobile Number*"
                            android:inputType="number"
                            android:maxLength="10"
                            android:paddingLeft="8dp"
                            android:paddingTop="15dp"
                            android:singleLine="true"
                            android:textColor="@color/colorPrimaryDark"
                            android:textColorHint="@color/colorPrimaryDark"
                            android:textSize="13dp" />

                    </LinearLayout>

                    <LinearLayout

                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal"
                        android:weightSum="10"

                        />

                    <LinearLayout

                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:orientation="horizontal">

                        <EditText

                            android:id="@+id/txtpassword"
                            android:layout_width="match_parent"
                            android:layout_height="50dp"
                            android:layout_weight="1"
                            android:hint="Password*"
                            android:inputType="textPassword"
                            android:maxLength="10"
                            android:paddingLeft="8dp"

                            android:paddingTop="15dp"

                            android:singleLine="true"
                            android:textColor="@color/colorPrimaryDark"
                            android:textColorHint="@color/colorPrimaryDark"
                            android:textSize="13dp" />

                    </LinearLayout>

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="111dp"
                        android:layout_height="30dp"
                        android:layout_marginTop="10dp"
                        android:text="  Register as*:"
                        android:textColor="#BE202F" />

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

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <RadioButton
                                android:id="@+id/rbdonor"
                                android:layout_width="171dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:text="I am a donor"
                                android:textColor="#BE202F" />

                            <RadioButton
                                android:id="@+id/rbneedy"
                                android:layout_width="183dp"
                                android:layout_height="wrap_content"
                                android:layout_weight="1"
                                android:text="I am in search of blood"
                                android:textColor="#BE202F" />
                        </LinearLayout>

                    </RadioGroup>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:layout_marginBottom="1dp"
                        android:orientation="horizontal">

                    </LinearLayout>

                    <Button
                        android:id="@+id/main_btn_reg"
                        android:layout_width="match_parent"
                        android:layout_height="45dp"
                        android:layout_marginTop="20dp"
                        android:background="@color/colorPrimaryDark"
                        android:text="REGISTER"
                        android:textColor="@color/white"
                        android:textSize="14dp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:orientation="vertical">

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="0.6dp"
                        android:layout_marginTop="16dp"
                        android:background="@color/colorPrimaryDark"></View>

                    <Button
                        android:id="@+id/btn_login"
                        android:layout_width="86dp"
                        android:layout_height="44dp"
                        android:layout_marginTop="7dp"
                        android:gravity="center"
                        android:text="LOGIN"
                        android:textColor="@color/colorPrimaryDark"
                        android:textSize="14dp" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="10dp"
                        android:orientation="vertical"></LinearLayout>

                </LinearLayout>

                <TextView
                    android:id="@+id/outputreg"
                    android:layout_width="match_parent"
                    android:layout_height="60dp" />

            </LinearLayout>
        </ScrollView>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout> ```

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题