安卓开发日记22

部分 Activity 类的实现,展示了如何初始化控件、处理点击事件以及简单的登录逻辑

// 请确保在正确的包名下创建该 Activity 类,并导入相应的类和资源

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class SignInActivity extends AppCompatActivity {

    private EditText inputEmail, inputPassword;
    private Button buttonSignIn;
    private TextView textCreateNewAccount;

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

        inputEmail = findViewById(R.id.inputEmail);
        inputPassword = findViewById(R.id.inputPassword);
        buttonSignIn = findViewById(R.id.buttonSignIn);
        textCreateNewAccount = findViewById(R.id.textCreateNewAccount);

        buttonSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signIn();
            }
        });

        textCreateNewAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理创建新账号的逻辑,此处仅做示例提醒
                Toast.makeText(SignInActivity.this, "Create new account clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void signIn() {
        String email = inputEmail.getText().toString().trim();
        String password = inputPassword.getText().toString().trim();

        if (TextUtils.isEmpty(email)) {
            inputEmail.setError("Please enter your email");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            inputPassword.setError("Please enter your password");
            return;
        }

        // TODO: 在此处添加用户验证和登录逻辑

        // 示例:显示登录成功提示
        Toast.makeText(this, "Sign in successful for " + email, Toast.LENGTH_SHORT).show();
    }
}
posted @ 2024-04-25 23:10  大虚胖子  阅读(3)  评论(0编辑  收藏  举报