- 本周的學習生活依舊非常的豐富,各個學科都學到了一些新的東西,特別是安卓的,讓我嘗到了學習它的樂趣,
- 本周的安卓學習了activity生命周期、事件處理及視窗的跳轉還有線性布局,事件處理及視窗的跳轉是真的讓我對這門課程產生了極大的興趣,因為作出來的效果都可以真真正正的在用戶界面看到,所以這讓我產生了一種成就感,原來我也可以做這么“厲害”的東西了,這給我繼續學習安卓這門課程極大的信心,
- activity生命周期 - activity生命周期的話,Activity類提供六個核心回呼方法:onCreate()、onStart()、onResume()、onPause()、onStop() 和 onDestroy(),當 Activity 進入新狀態時,系統會呼叫其中每個回呼方法,就是這些回呼方法很重要,但是因為有一些目前來說本人用得并不多,所以有一些還沒怎么太熟悉,在這里我插入一張華老師用過的圖片來進行一下總結,可能這六個核心的回呼方法就如下圖一樣,是相互對應的關系,

- 為了更好地查看相應應用的除錯資訊,需要添加訊息過濾器,下面這張圖片就是創建的程序,

事件處理及視窗的跳轉
- 這里用一個案例來進行總結,在本案例中,對相應的按鈕進行了相應的事件處理,并且,在登錄成功之后實作了視窗的跳轉,
- 以下是相應的步驟
1,創建一個新的專案


2,撰寫相應的代碼
activity_login的相應代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg2"
android:orientation="vertical"
android:gravity="center"
android:padding="15dp"
tools:context=".LoginActivity"
>
<TextView
android:id="@+id/tvUserLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_login"
android:textColor="#ff0000"
android:textSize="25sp"
android:layout_marginBottom="30dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/tvUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/username"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:id="@+id/edtUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/input_username"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/tvPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password"
android:textColor="#000000"
android:textSize="20sp"/>
<EditText
android:id="@+id/edtPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/input_password"
android:inputType="textPassword"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login"
android:textSize="20sp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textColor="#ffffff"
android:background="#04BE02"
android:layout_marginRight="10sp"
/>
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel"
android:textSize="20sp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textColor="#ffffff"
android:background="#04BE02"
/>
</LinearLayout>
</LinearLayout>
strings相應的代碼
<resources>
<string name="app_name">用戶登錄</string>
<string name="user_login">用戶登錄</string>
<string name="username">用戶:</string>
<string name="input_username">請輸入用戶名</string>
<string name="password">密碼:</string>
<string name="input_password">請輸入密碼</string>
<string name="login">登錄</string>
<string name="cancel">取消</string>
</resources>
LoginActivity相應的代碼
package net.zll.userlogin;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity {
private EditText edtUsername;
private EditText edtPassword;
private Button btnLogin;
private Button btnCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//利用布局資源檔案設定用戶界面
setContentView(R.layout.activity_login);
//通過資源標識獲得控制元件實體
edtUsername=findViewById(R.id.edtUsername);
edtPassword=findViewById(R.id.edtPassword);
btnLogin=findViewById(R.id.btnLogin);
btnCancel=findViewById(R.id.btnCancel);
//給登錄按鈕注冊監聽器,實作監聽器介面,撰寫事件處理方法
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//保存用戶輸入的資料:用戶名與密碼
String strUsername=edtUsername.getText().toString().trim();
String strPassword=edtPassword.getText().toString().trim();
//判斷用戶名與密碼是否正確(假定用戶名與密碼都是“admin”)
if(strUsername.equals("admin")&&strPassword.equals("admin")){
//利用吐司提示用戶登錄成功
Toast.makeText(LoginActivity.this,"恭喜,用戶名與密碼正確!",Toast.LENGTH_LONG).show();
//創建顯式意圖(引數1:包背景關系,引數2:目標組件)
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
//創建資料包,封裝資料
Bundle data=new Bundle();
data.putString("username",strUsername);
data.putString("password",strPassword);
//通過意圖攜帶資料包
intent.putExtras(data);
//按照意圖啟動目標組件
startActivity(intent);
}else {
//利用吐司提示用戶登錄失敗
Toast.makeText(LoginActivity.this,"遺憾,用戶名或密碼錯誤!",Toast.LENGTH_LONG).show();
}
}
});
//給取消按鈕注冊監聽器,實作監聽器介面,撰寫事件處理方法
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//關閉登錄視窗
finish();
}
});
// 在活動欄上顯示圖示
ActionBar actionBar=getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setLogo(R.mipmap.ic_launcher);
}
}
activity_main相應的代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center">
<TextView
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#ff0000"/>
</LinearLayout>
MainActivity相應的代碼
package net.zll.userlogin;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView tvMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通過資源識別符號獲取控制元件實體
tvMessage=findViewById(R.id.tvMessage);
//獲取意圖
Intent intent=getIntent();
//判斷意圖是否為空
if(intent!=null){
//獲取意圖攜帶的資料包
Bundle data=intent.getExtras();
//從資料包里按鍵取值獲取各項資料
String username=data.getString("username");
String password=data.getString("password");
//拼接用戶資訊
String message="登錄成功!\n\n用戶:"+username+"\n密碼:"+password+"\n\n歡迎您的到來!";
//設定標簽屬性,顯示用戶資訊
tvMessage.setText(message);
}
}
}
最后修改AndroidManifest,讓其首選啟動項為LoginActivity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.zll.userlogin">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>
</application>
</manifest>
3,效果


總結一下,學這里的時候還是非常的快樂的,畢竟有實際的東西出來,這個事件處理和視窗的跳轉的話不是很難,就是要做的東西比較的多,仔細一點的話是不會出太大的問題的,就是因為沒怎么做過,所以還是有點不太熟練,但是我相信后面慢慢得就會熟練起來的,
線性布局
線性布局就是對專案中相應的東西進行布局,讓它按照你想的位置安放,屬于比較基礎的內容,而已比較繁瑣,理解相應的命令是怎么用得就行,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/136834.html
標籤:其他
下一篇:Andoid:靜默安裝apk
