Android学习(三)--用户登录注册界面(界面跳转+背景音乐)

1.回顾

前两节,完成登录和注册界面的各项功能,接下来最后一项任务——欢迎界面,比较轻松一点,让我们Let's go.

2.欢迎界面

2.1界面展示

2.2欢迎界面功能简介

用户登录后,跳转到欢迎界面,会有一首背景音乐的播放,点燃你今日游戏的激情。通过本地数据库的数据存储,注册界面的用户名会传送到此界面,并表示欢迎,如下图。

在欢迎界面,有两个单选按钮,用户可选择今日想玩的游戏,而这个功能是RadioButton组件需要与RadioGroup组件一起使用,组成一个单选按钮,RadioGroup是可以容纳多个RadioButton的容器。最后选择的结果以提交的方式将单选按钮日志效果展示出来,如下图。

2.3界面代码

<?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"
    android:background="@drawable/bj2"
    tools:context=".Welcome">
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center"
        android:text="今日游戏清单"
        android:textSize="25sp"
        android:textColor="#516D2C" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@drawable/id"/>
        <TextView
            android:layout_weight="1"
            android:gravity="center"
            android:text="id"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@drawable/user"/>
        <TextView
            android:layout_weight="1"
            android:gravity="center"
            android:text="游戏"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@drawable/major"/>
        <TextView
            android:layout_weight="1"
            android:gravity="center"
            android:text="时间"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@drawable/isbn2"/>

        <TextView
            android:gravity="center"
            android:text="备注"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="54dp"
        android:gravity="center"
        android:text="你的选择是:"
        android:textColor="@color/black"
        android:textSize="25sp" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="63dp"
        android:gravity="center"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radio_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="王者荣耀"
            android:textColor="@color/black"
            android:textSize="25sp" />

        <RadioButton
            android:id="@+id/radio_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:text="天天酷跑"
            android:textColor="@color/black"
            android:textSize="25sp" />
    </RadioGroup>
    <Button
        android:id="@+id/bt_submit"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="提交"
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/welcome"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:textSize="25dp"
        tools:ignore="MissingConstraints" />
</LinearLayout>

2.4 java源码

package com.example.test06;

import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class Welcome extends AppCompatActivity {
    RadioGroup radioGroup;
    RadioButton radio_one,radio_two;    //单选按钮
    SharedPreferences sp;
    TextView showhello;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        initView();      //初始化控件
        clickEvent();     //点击事件
        sp = this.getSharedPreferences("username", this.MODE_PRIVATE);  //获取sharepreferences
        showhello = this.findViewById(R.id.welcome);            //显示欢迎
        showhello.setText("欢迎您!"+sp.getString("Loginname",""));     //获取用户名
    }
    private void initView() {
        radioGroup = findViewById(R.id.radioGroup);
        radio_one = findViewById(R.id.radio_one);
        radio_two = findViewById(R.id.radio_two);
    }

    private void clickEvent() {
        //给RadioGroup绑定监视器
        radioGroup.setOnCheckedChangeListener(new MyRadioButtonListener());
    }

    //单选按钮监听
    private class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // 选中状态改变时被触发
            switch (checkedId) {
                case R.id.radio_one:
                    Log.i("判断点击Id的单选按钮", "选择游戏为:"+radio_one.getText().toString());
                    break;
                case R.id.radio_two:
                    Log.i("判断点击Id的单选按钮", "选择游戏为:" + radio_two.getText().toString());
                    break;
            }
        }
    }

    //背景音乐
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MusicServer.stop(this);      //退出此页面会停止播放音乐
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MusicServer.play(this, R.raw.music);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_62250174/article/details/129344391
今日推荐