Android表单获取值的三种方式

本文介绍了在Android应用中,从表单获取用户输入值的三种常见方法,包括使用FindViewById、Data Binding库以及Kotlin的Anko库,详细解析了每种方式的实现步骤和适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

act_register.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="#f0f0f0"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注 册"
            android:textColor="#fff"
            android:textSize="44sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#fff"
        android:padding="11dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/reg_edit_user"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入用户名" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#fff"
        android:padding="11dp" >

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

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#fff"
        android:padding="11dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="重复密码"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/reg_edit_repwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请再次输入密码"
            android:inputType="textPassword" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#fff"
        android:padding="11dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电子邮箱"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/reg_edit_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入电子邮箱" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#fff"
        android:padding="11dp" >

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

        <RadioGroup
            android:id="@+id/reg_rg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/reg_rgman"
                style="@android:style/Widget.Holo.CompoundButton.Star"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="男" />

            <RadioButton
                android:id="@+id/reg_rgwoman"
                style="@android:style/Widget.Holo.CompoundButton.Star"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:text="女" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#fff" >

        <Button
            android:id="@+id/reg_but"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#6cf"
            android:onClick="onRegClick"
            android:text="提    交"
            android:textColor="#fff"
            android:textSize="22sp" />
    </LinearLayout>

</LinearLayout>

act_usercenter.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="#f0f0f0"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册成功"
            android:textColor="#fff"
            android:textSize="44sp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#fff"
        android:padding="12dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/text_user"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#f00"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#fff"
        android:padding="12dp" >

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

        <TextView
            android:id="@+id/text_pwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#f00"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#fff"
        android:padding="12dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电子邮箱"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/text_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#f00"
            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#fff"
        android:padding="12dp" >

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

        <TextView
            android:id="@+id/text_sex"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#f00"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>



RegisterActivity.java

package com.example.register;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class RegisterActivity extends Activity implements OnCheckedChangeListener{

	private RadioGroup rg;
	private RadioButton rgman,rgwoman;
	private String checked_rg = "男";
	
	private EditText ev_user,ev_pwd,ev_email;
	private TextView user,pwd,email,sex;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_register);
		initViews();
	}
	
	private void initViews() {
		// TODO Auto-generated method stub
		rg = (RadioGroup) findViewById(R.id.reg_rg);
		rg.setOnCheckedChangeListener(this);
		rgman = (RadioButton) findViewById(R.id.reg_rgman);
		rgwoman = (RadioButton) findViewById(R.id.reg_rgwoman);
		
		ev_user = (EditText) findViewById(R.id.reg_edit_user);
		ev_pwd = (EditText) findViewById(R.id.reg_edit_pwd);
		ev_email = (EditText) findViewById(R.id.reg_edit_email);
		
		
	}


	@Override
	public void onCheckedChanged(RadioGroup arg0, int arg1) {
		// TODO Auto-generated method stub
		if(arg1 == R.id.reg_rgman)
			checked_rg = "男";
		else
			checked_rg = "女";
	}
	
	
	public void onRegClick(View v){
		
		switch(v.getId()){
		case R.id.reg_but:
			/*AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("恭喜!注册成功!信息如下:");
			
			//一种提示信息,提示用户名、密码、邮箱等用户输入的信息
			builder.setMessage("用户名:" + ev_user.getText().toString() + 
					"\n密码:" + ev_pwd.getText().toString() + 
					"\n邮箱:" + ev_email.getText().toString() + 
					"\n性别:" + checked_rg);
			
			
			builder.setNegativeButton("确定", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					
				}
			});
			
			builder.show();*/
			
			
			//用Tost来做,Toast中显示一个新的Layout,并把用户输入的信息传过去
			Toast toast = new Toast(this);
			toast.setDuration(Toast.LENGTH_LONG);
			
			
			LayoutInflater inflater = this.getLayoutInflater();
			View view = inflater.inflate(R.layout.act_usercenter, null);
			
			
			/*((TextView)findViewById(R.id.text_user)).setText(ev_user.getText().toString());
			((TextView)findViewById(R.id.text_pwd)).setText(ev_pwd.getText().toString());
			((TextView)findViewById(R.id.text_sex)).setText(checked_rg);
			((TextView)findViewById(R.id.text_email)).setText(ev_email.getText().toString());*/
			
			user = (TextView)view.findViewById(R.id.text_user);
			pwd = (TextView) view.findViewById(R.id.text_pwd);
			email = (TextView) view.findViewById(R.id.text_email);
			sex = (TextView) view.findViewById(R.id.text_sex);
			
			user.setText(ev_user.getText().toString());
			pwd.setText(ev_pwd.getText().toString());
			email.setText(ev_email.getText().toString());
			sex.setText(checked_rg);
			
			
			toast.setView(view);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
			
			
			//第三种方法:Dialog中显示一个新的Layout,并把用户输入的信息传过去
			/*AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("fdsafds");
			
			LayoutInflater inflater = this.getLayoutInflater();
			View view = inflater.inflate(R.layout.act_usercenter, null);
			builder.setView(view);
			user = (TextView)view.findViewById(R.id.text_user);
			pwd = (TextView) view.findViewById(R.id.text_pwd);
			email = (TextView) view.findViewById(R.id.text_email);
			sex = (TextView) view.findViewById(R.id.text_sex);
			
			user.setText(ev_user.getText().toString());
			pwd.setText(ev_pwd.getText().toString());
			email.setText(ev_email.getText().toString());
			sex.setText(checked_rg);
			
			builder.show();*/
			
			
			break;
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值