表单(单选,复选,文本输入)

来源:互联网 发布:淘宝原创男包品牌 知乎 编辑:程序博客网 时间:2024/06/08 13:28

Layout


<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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical">             <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:textSize="20dp"                android:text="用户名:"/>            <EditText                android:id="@+id/userName"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:hint="请输入用户名"/>         </LinearLayout>         <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:textSize="20dp"                android:text="密    码:"/>            <EditText                android:id="@+id/password"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:hint="请输入密码"/>        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:textSize="20dp"                android:text="年    龄:"/>            <EditText                android:id="@+id/age"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:hint="请输入年龄"/>        </LinearLayout>        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="20dp"            android:text="性    别:"/>        <RadioGroup            android:id="@+id/sex"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="horizontal">            <RadioButton            android:id="@+id/girl"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="20dp"            android:checked="false"            android:text="Girl"/>            <RadioButton                android:id="@+id/boy"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:textSize="20dp"                android:checked="false"                android:text="Boy"/>         </RadioGroup>     </LinearLayout>     <LinearLayout         android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="20dp"            android:text="爱    好:"/>        <LinearLayout         android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="vertical">        <CheckBox            android:id="@+id/boxRead"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:checked="false"            android:textSize="20dp"            android:text="读书"/>        <CheckBox            android:id="@+id/boxFootball"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:checked="false"            android:textSize="20dp"            android:text="足球"/>        <CheckBox            android:id="@+id/boxBasketball"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:checked="false"            android:textSize="20dp"            android:text="篮球"/>      </LinearLayout>     </LinearLayout>     <LinearLayout         android:layout_width="match_parent"             android:layout_height="wrap_content"             android:gravity="center"             android:orientation="horizontal">        <Button            android:id="@+id/regedit"            android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="注册"/>        <Button            android:id="@+id/clean"            android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="清空"/>    </LinearLayout>    </LinearLayout></RelativeLayout>

java


package com.example.choose;import android.os.Bundle;import android.view.View;import android.app.Activity;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.RadioButton;import android.widget.Toast;public class MainActivity extends Activity {EditText edUserName,edPassword,edAge;Button btRegedit,btClean;RadioButton rbGirl,rbBoy;CheckBox cbRead,cbFootball,cbBasketball;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();btRegedit.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){String userName="",password="",age="",sex="",hobby="";userName=edUserName.getText().toString().trim();password=edPassword.getText().toString().trim();age=edAge.getText().toString().trim();if(age.equals("")||password.equals("")||userName.equals(""))Toast.makeText(getApplicationContext(), "请填写完整信息",Toast.LENGTH_SHORT).show();if(rbGirl.isChecked())sex="Girl";if(rbBoy.isChecked())sex="Boy";if(cbRead.isChecked())hobby=hobby+"reading"+"\n";if(cbFootball.isChecked())hobby=hobby+"football"+"\n";if(cbBasketball.isChecked())hobby=hobby+"basketball";Toast.makeText(getApplicationContext(),"你注册的信息是:\n"+        userName+"\n"+password+"\n"+        age+"\n"+sex+"\n"+        hobby+"\n", Toast.LENGTH_LONG).show();}});btClean.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {edUserName.setText("");edPassword.setText("");edAge.setText("");rbGirl.setChecked(false);rbBoy.setChecked(false);cbRead.setChecked(false);cbFootball.setChecked(false);cbBasketball.setChecked(false);}});}    private void init(){    edUserName=(EditText)findViewById(R.id.userName);    edPassword=(EditText)findViewById(R.id.password);    edAge=(EditText)findViewById(R.id.age);    btRegedit=(Button)findViewById(R.id.regedit);    btClean=(Button)findViewById(R.id.clean);    rbGirl=(RadioButton)findViewById(R.id.girl);    rbBoy=(RadioButton)findViewById(R.id.boy);    cbRead=(CheckBox)findViewById(R.id.boxRead);    cbFootball=(CheckBox)findViewById(R.id.boxFootball);    cbBasketball=(CheckBox)findViewById(R.id.boxBasketball);    }}


0 0
原创粉丝点击