IMG-LOGO
Trang Chủ Chủ Đề [Video] Bài giảng Activity, Intent và cách trao đổi dữ liệu qua Activity - Lập trình Android
× 1) [Share Code] Tạo dự án android đầu tay - Hello World - Lập trình Android 2. [Video] Bài giảng Activity, Intent và cách trao đổi dữ liệu qua Activity - Lập trình Android 3) [Video] Tìm hiểu ListView trong Android - Lập trình Android 4) [Video] Hướng dẫn tạo Food App bằng ListView + BaseAdapter - Lập trình android

[Video] Bài giảng Activity, Intent và cách trao đổi dữ liệu qua Activity - Lập trình Android

by TRẦN VĂN ĐIỆP - 18:39 25/11/2020 1,133 Lượt Xem


#AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gokisoft.c1812l">

    <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"></activity>
        <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=".RegisterActivity"></activity>
    </application>

</manifest>


#RegisterActivity.java


package com.gokisoft.c1812l;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {
    EditText txtFullname, txtUsername, txtPassword, txtConfirmPwd;
    Button btnBack, btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        txtFullname = findViewById(R.id.ar_txt_fullname);
        txtUsername = findViewById(R.id.ar_txt_username);
        txtPassword = findViewById(R.id.ar_txt_password);
        txtConfirmPwd = findViewById(R.id.ar_txt_confirm_password);
        btnBack = findViewById(R.id.ar_btn_back);
        btnSave = findViewById(R.id.ar_btn_save);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String fullname = txtFullname.getText().toString();
                String username = txtUsername.getText().toString();
                String pwd = txtPassword.getText().toString();
                String confirmPwd = txtConfirmPwd.getText().toString();

                if(username.isEmpty() || !pwd.equals(confirmPwd)) {
                    Toast.makeText(RegisterActivity.this, "Check inputs again!!!", Toast.LENGTH_SHORT).show();
                    return;
                }

                Intent intent = new Intent();
                intent.putExtra("fullname", fullname);
                intent.putExtra("username", username);
                intent.putExtra("pwd", pwd);
                intent.putExtra("confirmPwd", confirmPwd);

                setResult(LoginActivity.REQUEST_CODE_REGISTER, intent);
                finish();
            }
        });
    }
}


#MainActivity.java


package com.gokisoft.c1812l;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    TextView txtHello;
    Button btnTest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtHello = findViewById(R.id.txt_hello);
        btnTest = findViewById(R.id.button_test);

        txtHello.setText("OKOK");

        btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "TEST",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}


#LoginActivity.java


package com.gokisoft.c1812l;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    public static final int REQUEST_CODE_REGISTER = 1;

    EditText txtUsername, txtPassword;
    Button btnRegister, btnLogin;

    String username = "", password = "", fullname = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //mapping fields
        txtUsername = findViewById(R.id.al_txt_username);
        txtPassword = findViewById(R.id.al_txt_password);
        btnRegister = findViewById(R.id.al_btn_register);
        btnLogin = findViewById(R.id.al_btn_login);

        //dang ky su kien khi click regsiter & login
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //lam the de start activity tu activity nay
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, REQUEST_CODE_REGISTER);
            }
        });

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Toast.makeText(LoginActivity.this, "Check login", Toast.LENGTH_SHORT).show();
                String uname = txtUsername.getText().toString();
                String pwd = txtPassword.getText().toString();
                if(uname.equals(username) && pwd.equals(password)) {
                    Toast.makeText(LoginActivity.this, "Welcome " + fullname, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LoginActivity.this, "Login failed!!!", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode) {
            case REQUEST_CODE_REGISTER:
                //du lieu duoc tra ve tu RegisterActivity
                if(data != null) {
                    fullname = data.getStringExtra("fullname");
                    username = data.getStringExtra("username");
                    password = data.getStringExtra("pwd");
                }
                break;
        }
    }
}


Theo dõi cập nhật nội dung học trên Youtube


Bình luận



Chia sẻ từ lớp học

Quick Quizzz!!!

User ImageUser ImageUser Image
User ImageUser ImageUser Image
User ImageUser ImageUser Image
User ImageUser ImageUser Image
User ImageUser ImageUser Image
User Image
User ImageUser ImageUser Image
>> Hiển Thị Quizzz
Đã sao chép!!!