安卓 登录案例

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="username" />

        <EditText
            android:id="@+id/edit1"
            android:layout_width="150dp"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="password" />

        <EditText
            android:id="@+id/edit2"
            android:layout_width="150dp"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="登录" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="注册" />
    </LinearLayout>

</LinearLayout>
EditText username;
	EditText password;
	Button login;
	Button reg;

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

		username = (EditText) findViewById(R.id.edit1);
		password = (EditText) findViewById(R.id.edit2);
		login = (Button) findViewById(R.id.btn1);
		reg = (Button) findViewById(R.id.btn2);

		// 匿名
		login.setOnClickListener(new OnClickListener() {
    
    

			@Override
			public void onClick(View arg0) {
    
    
				if (username.getText().toString().equals("")|| password.getText().toString().equals("")) {
    
    
					Toast.makeText(MainActivity.this, "用户名或密码不能为空!!!", 1).show();
				} else if (username.getText().toString().equals("666")|| password.getText().toString().equals("666")) {
    
    
					Toast.makeText(MainActivity.this, "登录成功", 1).show();
				} else {
    
    
					Toast.makeText(MainActivity.this, "账号或密码错误!!!", 1).show();
				}

			}
		});
	}

猜你喜欢

转载自blog.csdn.net/zx77588023/article/details/115312225