Android实现简单的登录界面并实现数据的成块传递

  首先新建一个工程,在MainActivity布局里面做如下配置:

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="username:"
            android:gravity="center"
            />

        <EditText
            android:id="@+id/name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Please input your name"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="password:"
            android:gravity="center"/>

        <EditText
            android:id="@+id/password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Please input your password"/>

    </LinearLayout>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="性别"
        android:layout_marginLeft="30dp"
        android:orientation="horizontal"
        android:layout_marginTop="20dp">

        <RadioButton
            android:id="@+id/sex_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:checked="true"
            android:text="男"/>

        <RadioButton
            android:id="@+id/sex_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="110dp"
            android:text="女"/>

    </RadioGroup>

    <Button
        android:id="@+id/login_btn"
        android:layout_width="200dp"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Login"/>


</LinearLayout>

做出来的效果图就是这样子的

然后改写MainActivity

package com.jikexueyuan.myandroidthree;

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

public class MainActivity extends AppCompatActivity {

    private EditText name;
    private EditText password;
    private RadioButton male,female;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main);
//当登录按钮被点击 findViewById(R.id.login_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { loginDate(); } }); } //传递登录数据 public void loginDate(){ name=findViewById(R.id.name); password=findViewById(R.id.password); male=findViewById(R.id.sex_male); female=findViewById(R.id.sex_female); String sex; //判断单选按钮 if(male.isChecked()){ sex="男"; }else{ sex="女"; } Intent i=new Intent(MainActivity.this,LoginAfterActivity.class); Bundle bundle=new Bundle(); bundle.putString("name",name.getText().toString()); bundle.putString("password",password.getText().toString()); bundle.putString("sex",sex); i.putExtras(bundle); startActivity(i); } }

下一步就是创建一个活动来接收并显示传递的值,就命名为LoginAfterActivity,首先在新建的活动的布局中设置三个TextView,

<?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"
    android:orientation="vertical"
    tools:context=".LoginAfterActivity">

    <TextView
        android:id="@+id/e_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/e_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/e_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <Button
        android:id="@+id/return_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="返回登录界面"/>
</LinearLayout>

  这里我多加了一个按钮,用于返回上一个界面,最后我们只需要在LoginAfterActivity中改写代码就行了

package com.jikexueyuan.myandroidthree;

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

public class LoginAfterActivity extends AppCompatActivity {

    private TextView name,password,sex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_login_after); name
=findViewById(R.id.e_name); password=findViewById(R.id.e_password); sex=findViewById(R.id.e_sex); //得到上一个页面传过来的值 Intent i=getIntent(); Bundle b=i.getExtras(); name.setText(String.format("姓名:%S",b.getString("name").toString())); password.setText(String.format("密码:%S",b.getString("password").toString())); sex.setText(String.format("性别:%S",b.getString("sex").toString())); //点击返回按钮返回上一界面 findViewById(R.id.return_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //销魂当前活动,返回上一活动 finish(); } }); } }

  最后的运行结果就是这样的

主界面

 

   点击LOGIN之后

点击返回登录界面按钮之后,就会返回登录界面。

 

posted @ 2019-03-18 09:36  羽吢  阅读(365)  评论(0)    收藏  举报