安卓开发日记21

主要使用了ScrollView包裹着一个垂直方向的LinearLayout,其中包含了一些TextView、EditText和一个登录按钮。

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) {
                // Handle creating new account
                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: Implement your sign-in logic here, such as calling an authentication method

        // For demonstration purposes, show a toast indicating sign-in success
        Toast.makeText(this, "Sign in successful for " + email, Toast.LENGTH_SHORT).show();
    }
}
这段代码展示了如何在Activity中初始化和处理登录页面的元素,并添加了点击登录按钮和创建新账号文本的事件处理逻辑。
posted @ 2024-04-25 23:10  大虚胖子  阅读(3)  评论(0编辑  收藏  举报