目錄
1.功能要求
2.功能實現(xiàn)流程圖
3.功能演示
4.界面與功能
?4.1登錄界面
4.1.1界面展示
4.1.2登錄界面功能簡介
4.1.3界面代碼
4.1.4登錄按鈕點擊事件
4.1.5退出按鈕點擊事件
?4.1.6背景音樂點擊事件
4.1.7記住密碼
5.Java源碼
1.功能要求
(1)三個界面布局,體現(xiàn)文本框、編輯框、單選按鈕、復選框按鈕、按鈕、listview控件,其它控件可自選。
(2)實現(xiàn)從第一個界面分別能跳轉(zhuǎn)到另兩個界面,其它兩個界面也可返回第一個界面,將一個界面的數(shù)據(jù)通過Activity的數(shù)據(jù)傳遞傳送到另一個界面。
(3)獲取某個界面的數(shù)據(jù),將數(shù)據(jù)存儲到外部文件,數(shù)據(jù)存儲方式可自選。
(4)根據(jù)各自項目的需求在某個界面中通過自定義廣播、Toast提示實現(xiàn)一條廣播的發(fā)送。
(5)在第一個界面當點擊按鈕時利用服務、MediaPlayer實現(xiàn)播放、停止播放背景音樂的功能。
2.功能實現(xiàn)流程圖
3.功能演示
功能演示
4.界面與功能
?4.1登錄界面
4.1.1界面展示
4.1.2登錄界面功能簡介
在登錄界面可以實現(xiàn)5個功能:
(1)登錄界面的跳轉(zhuǎn)(2)注冊界面的跳轉(zhuǎn)(3)退出功能
(4)記住密碼功能(5)背景音樂播放和停止功能
界面跳轉(zhuǎn)功能我使用的是intent實現(xiàn)activity與activity之間的跳轉(zhuǎn),可以直接按返回鍵返回到前一頁,不需要自己添加按鍵監(jiān)聽代碼實現(xiàn),但需要在manifest注冊activity。
4.1.3界面代碼
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bj1"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginEnd="32dp"
android:hint="請輸入用戶名"
app:layout_constraintBottom_toTopOf="@+id/pwd"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
android:maxLines="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.972" />
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginEnd="32dp"
android:hint="請輸入密碼"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
android:maxLines="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.546" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="用戶登錄"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.89" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:text="用戶:"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/pwd"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/name"
app:layout_constraintVertical_bias="0.37" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:text="密碼:"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/pwd"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:layout_constraintVertical_bias="0.718" />
<Button
android:id="@+id/login"
style="@style/AlertDialog.AppCompat.Light"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="30dp"
android:layout_marginTop="32dp"
android:layout_marginRight="30dp"
android:backgroundTint="@android:color/holo_blue_dark"
android:text="登錄"
app:layout_constraintEnd_toStartOf="@+id/quit"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pwd" />
<Button
android:id="@+id/reg"
style="@style/AlertDialog.AppCompat.Light"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="30dp"
android:layout_marginTop="76dp"
android:layout_marginRight="30dp"
android:backgroundTint="@android:color/holo_blue_dark"
android:text="注冊"
app:layout_constraintEnd_toStartOf="@+id/quit"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pwd" />
<Button
android:id="@+id/quit"
style="@style/AlertDialog.AppCompat.Light"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center_horizontal"
android:layout_marginStart="30dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="30dp"
android:backgroundTint="@android:color/darker_gray"
android:text="退出"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pwd" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:text="記住密碼"
app:layout_constraintStart_toStartOf="@+id/login"
app:layout_constraintTop_toBottomOf="@+id/pwd" />
<ImageButton
android:id="@+id/imagbutton"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#00FF0000"
android:onClick="onclickPlay"
android:scaleType="centerInside"
android:src="@drawable/bf" />
</androidx.constraintlayout.widget.ConstraintLayout>
4.1.4登錄按鈕點擊事件
實現(xiàn)按鈕的點擊事件,在java代碼中使用的是匿名內(nèi)部類,給Button綁定一個監(jiān)聽事件,并監(jiān)聽一個點擊事件,在setOnClickListener方法中直接傳入一個OnClickListener對象,并實現(xiàn)OnClick方法。
點擊登錄按鈕,如果用戶編輯框的數(shù)據(jù)跟密碼編輯框的數(shù)據(jù)相對應,就會跳轉(zhuǎn)到歡迎界面;如果數(shù)據(jù)不對應,將會有toast彈窗提示“密碼錯誤或用戶不存在!”,如下圖。
java代碼實現(xiàn)
//登錄事件
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = name.getText().toString();
String password = pwd.getText().toString(); //獲取用戶輸入的用戶名和密碼
//查詢用戶名和密碼相同的數(shù)據(jù)
Cursor cursor = db.query("logins",new String[]{"usname","uspwd"}," usname=? and uspwd=?",new String[]{username,password},null,null,null);
int flag = cursor.getCount(); //查詢出來的記錄項的條數(shù),若沒有該用戶則為0條
if(flag!=0){ //若查詢出的記錄不為0,則進行跳轉(zhuǎn)操作
Intent intent = new Intent();
intent.setClass(MainActivity.this,Welcome.class); //設置頁面跳轉(zhuǎn)
SharedPreferences.Editor editor = sp2.edit();
cursor.moveToFirst(); //將光標移動到position為0的位置,默認位置為-1
String loginname = cursor.getString(0);
editor.putString("Loginname",loginname);
editor.commit(); //將用戶名存到SharedPreferences中
startActivity(intent);
}
else{
Toast.makeText(MainActivity.this,"密碼錯誤或用戶不存在!",Toast.LENGTH_LONG).show(); //提示用戶信息錯誤或沒有賬號
}
}
});
4.1.5退出按鈕點擊事件
退出事件可由兩種方式實現(xiàn):
- 點擊退出按鈕退出,會有彈框彈出來提示用戶是否退出程序。
- 返回鍵退出,在兩秒內(nèi)點擊返回鍵實現(xiàn)退出程序。
如下圖
?java代碼實現(xiàn)文章來源:http://www.zghlxwxcb.cn/news/detail-448241.html
//退出事件
back.setOnClickListener(new View.OnClickListener() { //為退出按鈕添加監(jiān)聽事件實現(xiàn)退出(用到彈框提示確認退出)
@Override
public void onClick(View v) {
//創(chuàng)建彈框?qū)ο?顯示在當前頁面
AlertDialog.Builder ab = new AlertDialog.Builder(MainActivity.this);
//編輯彈框樣式
ab.setTitle("提示"); //創(chuàng)建標題
ab.setIcon(R.mipmap.ic_launcher_round); //設置圖標
ab.setMessage("您是否確定退出?"); //設置內(nèi)容
//設置按鈕
ab.setPositiveButton("取消",null);
ab.setNeutralButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 實現(xiàn)程序的退出,結(jié)束當前
MainActivity.this.finish();
}
});
//創(chuàng)建彈框
ab.create();
//顯示彈框
ab.show();
}
});
}
//2秒內(nèi)點擊兩次返回鍵退出
long exittime; //設定退出時間間隔
public boolean onKeyDown(int keyCode, KeyEvent event){ //參數(shù):按的鍵;按鍵事件
//判斷事件觸發(fā)
if (keyCode == KeyEvent.KEYCODE_BACK){
//判斷兩次點擊間隔時間
if((System.currentTimeMillis()-exittime)>2000){
Toast.makeText(MainActivity.this,"再次返回程序退出!",Toast.LENGTH_SHORT).show();
exittime = System.currentTimeMillis(); //設置第一次點擊時間
}else{
finish();
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode,event);
?4.1.6背景音樂點擊事件
背景音樂的播放和暫停功能,這里使用的是soundpool實現(xiàn)背景音樂播放功能,soundpool類可用于管理和播放應用中的音頻資源,這些音頻資源可以放在存儲文件中也可以包含在程序中,但實現(xiàn)播放的時間比較短。在這兒之前還需在res路徑下新建一個raw的文件夾,并將提前準備好的mp3存放在此,如下圖。
?java代碼實現(xiàn)
//背景音樂
public void playSound(int sound, int loop) { //獲取AudioManager引用
AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
//獲取當前音量
float streamVolumeCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
//獲取系統(tǒng)最大音量
float streamVolumeMax = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//計算得到播放音量
float volume = streamVolumeCurrent / streamVolumeMax;
//調(diào)用SoundPool的play方法來播放聲音文件
currStaeamId = ms.play(music.get(sound), volume, volume, 1, loop, 1.0f);
}
public void initSoundPool() {//初始化聲音池
ms = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); //創(chuàng)建SoundPool對象
music = new HashMap<Integer, Integer>(); //創(chuàng)建HashMap對象
//加載聲音文件,并且設置為1號聲音放入hm中
music.put(1, ms.load(this, R.raw.bjmusic, 1));
}
public void onclickPlay(View view){
//點擊按鈕播放音樂,再次點擊停止播放
if(cnt==0){
cnt = 1;
}
else {
cnt = 0;
}
if(cnt==1) {
playSound(1,0); //播放1號聲音資源,且播放一次
//提示播放
Toast.makeText(MainActivity.this, "播放音效", Toast.LENGTH_SHORT).show();
}
else {
ms.stop(currStaeamId); //停止正在播放的聲音
//提示停止播放
Toast.makeText(MainActivity.this, "停止播放音效", Toast.LENGTH_SHORT).show();
}
}
4.1.7記住密碼
使用sharedPreferencers方法實現(xiàn)記住密碼功能,在登錄界面輸入內(nèi)容后,勾選記住密碼復選框,然后再點擊登錄,登錄成功后將軟件關掉,再重新打開,就會看到賬號和密碼就已經(jīng)輸入在框內(nèi)了,記住的密碼將會保存在Device File Explorer視圖下的data/data/com.example.test06/shared_prefs文件中,如下圖。
?java代碼實現(xiàn)
//從SharedPreferences中獲取是否記住密碼的參數(shù)
final SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(this);
boolean isRemember = preference.getBoolean("remember_pwd", false);
//設置賬號與密碼到文本框,并勾選記住密碼
if (isRemember) {
name.setText(preference.getString("name", ""));
pwd.setText(preference.getString("password", ""));
remember_pwd.setChecked(true);
}
5.Java源碼
package com.example.test06;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
EditText name,pwd;
Button btnlogin,btnreg,back;
CheckBox remember_pwd,show,editText;
Mysql mysql;
SQLiteDatabase db;
SharedPreferences sp1,sp2;
ImageButton btn_play;
public static int cnt = 0;
SoundPool ms; //聲明SoundPool的引用
HashMap<Integer, Integer> music; //聲明HashMap來存放聲音文件
int currStaeamId; //當前正播放的streamId
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = this.findViewById(R.id.name); //用戶名輸入框
pwd = this.findViewById(R.id.pwd); //密碼輸入框
btnlogin = this.findViewById(R.id.login); //登錄按鈕
btnreg = this.findViewById(R.id.reg); //注冊按鈕
back = this.findViewById(R.id.quit); //退出按鈕
btn_play = this.findViewById(R.id.imagbutton); //背景音樂按鈕
remember_pwd = this.findViewById(R.id.checkBox); //復選按鈕
sp1 = this.getSharedPreferences("useinfo",this.MODE_PRIVATE);
sp2 = this.getSharedPreferences("username",this.MODE_PRIVATE);
initSoundPool();//初始化聲音池的方法
name.setText(sp1.getString("usname",null));
pwd.setText(sp1.getString("uspwd",null));
mysql = new Mysql(this,"Userinfo",null,1); //建立數(shù)據(jù)庫
db = mysql.getReadableDatabase();
//從SharedPreferences中獲取是否記住密碼的參數(shù)
final SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(this);
boolean isRemember = preference.getBoolean("remember_pwd", false);
//設置賬號與密碼到文本框,并勾選記住密碼
if (isRemember) {
name.setText(preference.getString("name", ""));
pwd.setText(preference.getString("password", ""));
remember_pwd.setChecked(true);
}
//登錄事件
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = name.getText().toString();
String password = pwd.getText().toString(); //獲取用戶輸入的用戶名和密碼
//查詢用戶名和密碼相同的數(shù)據(jù)
Cursor cursor = db.query("logins",new String[]{"usname","uspwd"}," usname=? and uspwd=?",new String[]{username,password},null,null,null);
int flag = cursor.getCount(); //查詢出來的記錄項的條數(shù),若沒有該用戶則為0條
if(flag!=0){ //若查詢出的記錄不為0,則進行跳轉(zhuǎn)操作
Intent intent = new Intent();
intent.setClass(MainActivity.this,Welcome.class); //設置頁面跳轉(zhuǎn)
SharedPreferences.Editor editor = sp2.edit();
cursor.moveToFirst(); //將光標移動到position為0的位置,默認位置為-1
String loginname = cursor.getString(0);
editor.putString("Loginname",loginname);
editor.commit(); //將用戶名存到SharedPreferences中
startActivity(intent);
}
else{
Toast.makeText(MainActivity.this,"密碼錯誤或用戶不存在!",Toast.LENGTH_LONG).show(); //提示用戶信息錯誤或沒有賬號
}
}
});
//注冊事件
btnreg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this,Register.class); //跳轉(zhuǎn)到注冊頁面
startActivity(intent);
Toast.makeText(MainActivity.this,"前往注冊!",Toast.LENGTH_SHORT).show();
}
});
//退出事件
back.setOnClickListener(new View.OnClickListener() { //為退出按鈕添加監(jiān)聽事件實現(xiàn)退出(用到彈框提示確認退出)
@Override
public void onClick(View v) {
//創(chuàng)建彈框?qū)ο?顯示在當前頁面
AlertDialog.Builder ab = new AlertDialog.Builder(MainActivity.this);
//編輯彈框樣式
ab.setTitle("提示"); //創(chuàng)建標題
ab.setIcon(R.mipmap.ic_launcher_round); //設置圖標
ab.setMessage("您是否確定退出?"); //設置內(nèi)容
//設置按鈕
ab.setPositiveButton("取消",null);
ab.setNeutralButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 實現(xiàn)程序的退出,結(jié)束當前
MainActivity.this.finish();
}
});
//創(chuàng)建彈框
ab.create();
//顯示彈框
ab.show();
}
});
}
//2秒內(nèi)點擊兩次返回鍵退出
long exittime; //設定退出時間間隔
public boolean onKeyDown(int keyCode, KeyEvent event){ //參數(shù):按的鍵;按鍵事件
//判斷事件觸發(fā)
if (keyCode == KeyEvent.KEYCODE_BACK){
//判斷兩次點擊間隔時間
if((System.currentTimeMillis()-exittime)>2000){
Toast.makeText(MainActivity.this,"再次返回程序退出!",Toast.LENGTH_SHORT).show();
exittime = System.currentTimeMillis(); //設置第一次點擊時間
}else{
finish();
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode,event);
}
//背景音樂
public void playSound(int sound, int loop) { //獲取AudioManager引用
AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
//獲取當前音量
float streamVolumeCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);
//獲取系統(tǒng)最大音量
float streamVolumeMax = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//計算得到播放音量
float volume = streamVolumeCurrent / streamVolumeMax;
//調(diào)用SoundPool的play方法來播放聲音文件
currStaeamId = ms.play(music.get(sound), volume, volume, 1, loop, 1.0f);
}
public void initSoundPool() {//初始化聲音池
ms = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); //創(chuàng)建SoundPool對象
music = new HashMap<Integer, Integer>(); //創(chuàng)建HashMap對象
//加載聲音文件,并且設置為1號聲音放入hm中
music.put(1, ms.load(this, R.raw.bjmusic, 1));
}
public void onclickPlay(View view){
//點擊按鈕播放音樂,再次點擊停止播放
if(cnt==0){
cnt = 1;
}
else {
cnt = 0;
}
if(cnt==1) {
playSound(1,0); //播放1號聲音資源,且播放一次
//提示播放
Toast.makeText(MainActivity.this, "播放音效", Toast.LENGTH_SHORT).show();
}
else {
ms.stop(currStaeamId); //停止正在播放的聲音
//提示停止播放
Toast.makeText(MainActivity.this, "停止播放音效", Toast.LENGTH_SHORT).show();
}
}
}
未完待續(xù)文章來源地址http://www.zghlxwxcb.cn/news/detail-448241.html
到了這里,關于Android學習(一)--用戶登錄注冊界面(界面跳轉(zhuǎn)+背景音樂)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!