By GokiSoft.com|
21:25 10/03/2021|
Android
[Share Code] Tìm hiểu về Component trong Android - Activity trong Android
#build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "gokisoft.j2010g"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
}
#AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gokisoft.j2010g">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TestActivity"></activity>
</application>
</manifest>
#MainActivity.java
package gokisoft.j2010g;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//Mapping object trong XML sang Java
EditText edEmail, edPassword;
Button btnSave, btnReset, btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Mapping object java & component trong xml
edEmail = findViewById(R.id.am_ed_email);
edPassword = findViewById(R.id.am_ed_password);
btnSave = findViewById(R.id.am_btn_save);
btnReset = findViewById(R.id.am_btn_reset);
btnStart = findViewById(R.id.am_btn_start);
//Bat su kien khi nguoi dung click vao button save & reset
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//noi xu ly code -> khi click vao button save
String email = edEmail.getText().toString();
String pwd = edPassword.getText().toString();
Log.d("MainActivity", email);
Log.d(MainActivity.class.getName(), pwd);
//Gia su neu email: aptech@com & pwd: 123 => show message: Login success!
//Show message: Login failed.
if(email.equals("aptech@com") && pwd.equals("123")) {
//login success
Toast.makeText(MainActivity.this, "Login success", Toast.LENGTH_SHORT).show();
} else {
//login failed
Toast.makeText(MainActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
}
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//noi xu ly code -> khi click vao button reset
edEmail.setText("");
edPassword.setText("");
}
});
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent in = new Intent(MainActivity.this, TestActivity.class);
startActivity(in);
}
});
}
}
#TestActivity.java
package gokisoft.j2010g;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}
}
#activity_main.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"
android:orientation="vertical"
tools:context="gokisoft.j2010g.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textAlignment="center"
android:textColor="@color/colorRed"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp"
android:text="Email:" />
<EditText
android:id="@+id/am_ed_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:padding="10dp"
android:hint="Enter email"
android:textSize="16dp"
android:inputType="textPersonName"
android:text="" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:padding="10dp"
android:text="Password:" />
<EditText
android:id="@+id/am_ed_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="16dp"
android:padding="10dp"
android:hint="Enter password"
android:inputType="textPersonName"
android:text="" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<Button
android:id="@+id/am_btn_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save Data" />
<Button
android:id="@+id/am_btn_reset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reset Data" />
</LinearLayout>
<Button
android:id="@+id/am_btn_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Test Activity" />
</LinearLayout>
#activity_test.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"
android:orientation="vertical"
tools:context="gokisoft.j2010g.TestActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="50dp"
android:textColor="@color/colorRed"
android:textAlignment="center"
android:text="Welcome to learn Android" />
</LinearLayout>
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)