您的位置:首页 > 其它

安卓开发实例(2)实现用户注册

2017-03-12 19:20 645 查看

目的

掌握Activity两个界面数据的相互传递

总结

(1)初步接触了线性布局模式

(2)明白了xml与Acivity的关系,xml是视图,Acitivity是控制

,把视图之中的id传给控制代码,控制代码就可以控制视图,监听其界面,实现其功能。

(3)可运用Intent中的putExtra()传送数据,getStringIntent()接收数据。

putExtra()方法示例:

intent.putExtra("name",et_name.getText().toString().trim());//存储输入的用户名在name里
intent.putExtra("password",et_password.getText().toString().trim());//存储输入的密码在password里


getStringintent()方法示例:

String name = intent.getStringExtra("name");
String password = intent.getStringExtra("password");


putExtra中传送的数据需与getStringIntent中接收的数据相对应。

结果展示

1.打开app,显示页面如下



2.输入用户名、密码



3.显示用户名、密码



步骤与代码

1.创建一个MainActivity

package com.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {
private RadioButton manRadio;//选择男
private RadioButton womanRadio;//选择女
private EditText et_name;//输入的用户名
private EditText et_password;//输入的密码
private Button btn_send;//发送按钮

@Override
protected void onCreate(Bundle savedInstanceState) {//类似于构造函数
super.onCreate(savedInstanceState);
//初始化赋值
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
et_password = (EditText)findViewById(R.id.et_password);
btn_send = (Button) findViewById(R.id.btn_send);
manRadio = (RadioButton) findViewById(R.id.radioMale);
womanRadio = (RadioButton)findViewById(R.id.radioFemale);
//点击提交用户信息按钮进行数据传递
btn_send.setOnClickListener(new View.OnClic
d670
kListener() {
@Override
public void onClick(View view) {
passDate();
}
});
}
public void passDate(){
//创建Intent对象,启动Main2Activity
Intent intent = new Intent(this,Main2Activity.class);
//将数据存入Intent对象
//public String toString()返回该对象的字符串表示。
// 通常,ToString方法会返回一个“以文本方式表示”此对象的字符串。结果应是一个简明但易于读懂的信息表达式。
//trim()[1] 函数移除字符串两侧的空白字符或其他预定义字符。

intent.putExtra("name",et_name.getText().toString().trim());//存储输入的用户名在name里 intent.putExtra("password",et_password.getText().toString().trim());//存储输入的密码在password里
String str = "";
if(manRadio.isChecked()){
str = "男";
}else if(womanRadio.isChecked()){
str = "女";
}
intent.putExtra("sex",str);//存储输入的性别在sex里
startActivity(intent);//跳转到另一个activity

}
}


2.对应的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.myapplication.MainActivity">

<LinearLayout
android:id = "@+id/regist_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal = "true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:orientation="horizontal">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5dp"
android:text="用户名:"/>
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入你的用户名:"
android:textSize="14dp"/>
</LinearLayout>
<LinearLayout
android:id = "@+id/regist_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/regist_username"
android:layout_centerHorizontal = "true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5dp"
android:text="密  码:"/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入你的密码:"
android:inputType="textPassword"
android:textSize="14dp"/>
</LinearLayout>
<RadioGroup
android:id = "@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/regist_password"
android:layout_marginLeft="30dp"
android:contentDescription="性别"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男"/>
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="提交用户信息"/>

</RelativeLayout>


3.跳转的Main2Activity代码

package com.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {
private TextView tv_name,tv_password,tv_sex;//视图上的用户名、密码、性别
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//获取Intent对象
Intent intent = getIntent();
//取出key对应的value值
String name = intent.getStringExtra("name"); String password = intent.getStringExtra("password");
String sex = intent.getStringExtra("sex");
//初始化视图界面的信息
tv_name = (TextView) findViewById(R.id.tv_name);
tv_password = (TextView)findViewById(R.id.tv_password);
tv_sex = (TextView) findViewById(R.id.tv_sex);
//传值、显示
tv_name.setText("用户名:"+name);
tv_password.setText("密码:"+password);
tv_sex.setText("性别:"+sex);
}
}


4.Main2Activity的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapplication.Main2Activity">

<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:textSize = "20dp"/>
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:textSize = "20dp"/>
<TextView
android:id="@+id/tv_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:textSize = "20dp"/>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐