package com.example.demo;

import android.os.Bundle;
import android.provider.MediaStore.Audio.Radio;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.AdapterView;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    
    private EditText account;
    private EditText password;
    private RadioGroup gender;
    private Spinner classroom;
    private Button submit;
    private String[] classnames = {"1班","2班"};
    
    
    private ArrayAdapter<String> adapter;
    

    private String myname;
    private String mypsw;
    private String myclass;
    private String mysex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item,classnames);
        
        classroom.setAdapter(adapter);
        myclass = classnames[0]; 
        
        submit.setOnClickListener(submitL);
        classroom.setOnItemSelectedListener(classroomL);
        gender.setOnCheckedChangeListener(genderL);
    };
    
    RadioGroup.OnCheckedChangeListener genderL = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton rad = (RadioButton) findViewById(checkedId);
            mysex = rad.getText().toString();
        }
    };

    OnClickListener submitL = new OnClickListener() {
        @Override
        public void onClick(View v) {
            myname = account.getText().toString().trim();
            mypsw = password.getText().toString().trim();
            
            if (myname == null || mysex.length() == 0) {
                Toast.makeText(MainActivity.this, "姓名空", Toast.LENGTH_SHORT).show();
                return;
            }
            
            if (mypsw == null || mysex.length() == 0) {
                Toast.makeText(MainActivity.this, "密码空", Toast.LENGTH_SHORT).show();
                return;
            }
            
            if (mysex == null || mysex.length() == 0) {
                Toast.makeText(MainActivity.this, "性别空", Toast.LENGTH_SHORT).show();
                return;
            }
            
            if (myclass == null || myclass.length() == 0) {
                Toast.makeText(MainActivity.this, "班级空", Toast.LENGTH_SHORT).show();
                return;
            }
            
            StringBuilder userInfo = new StringBuilder();
            userInfo.append("姓名").append(myname).append("\n");
            userInfo.append("密码").append(mypsw).append("\n");
            userInfo.append("性别").append(mysex).append("\n");
            userInfo.append("班级").append(myclass);
            
            Toast.makeText(MainActivity.this, userInfo.toString(), Toast.LENGTH_LONG).show();
        }
    };

    AdapterView.OnItemSelectedListener classroomL = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            myclass = classnames[position];
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    };

    private void init() {
        account = (EditText) this.findViewById(R.id.account);
        password = (EditText) this.findViewById(R.id.password);
        classroom = (Spinner) this.findViewById(R.id.classroom);
        gender = (RadioGroup) this.findViewById(R.id.gender);
        submit = (Button) this.findViewById(R.id.submit);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

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

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="账号" />

            <EditText
                android:id="@+id/account"
                android:layout_width="200sp"
                android:layout_height="wrap_content"
                android:hint="请输入账号" />
        </LinearLayout>

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密码" />

            <EditText
                android:id="@+id/password"
                android:layout_width="200sp"
                android:layout_height="wrap_content"
                android:hint="请输入密码" />
        </LinearLayout>

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="性别" />

            <RadioGroup
                android:id="@+id/gender"
                android:layout_width="200sp"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="男" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="女" />
            </RadioGroup>
        </LinearLayout>

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="班级" />
            <Spinner 
                android:id="@+id/classroom"
                android:layout_width="200sp"
                android:layout_height="wrap_content"
                />
        </LinearLayout>
        
        
         <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button 
                android:id="@+id/submit"
                android:layout_width="200sp"
                android:layout_height="wrap_content"
                android:text="提交"/>
        </LinearLayout>
        
    </LinearLayout>

</RelativeLayout>
最后修改:2025 年 03 月 10 日
如果觉得我的文章对你有用,请随意赞赏