android学习日志

任务二实现点击时间和手势联系
1.了解三个基本控件

  • 标签控件(TextView)
  • 编辑框控件(EditText)
  • 按钮控件(Button)

2.创建登录界面
创建一个名为LoginActiity的空项目,在activity_login.xml写入代码

<?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:background="@drawable/background"
    android:gravity="center"
    android:padding="15dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvUserLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:text="@string/user_login"
        android:textColor="#ff00ff"
        android:textSize="25sp" />

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

        <TextView
            android:id="@+id/tvUsername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/username"
            android:textColor="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/edtUsername"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/input_username"
            android:singleLine="true" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/tvPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/password"
            android:textColor="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/edtPassword"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/input_password"
            android:inputType="textPassword"
            android:singleLine="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:text="@string/login"
            android:textSize="20sp" />

        <Button
            android:id="@+id/btnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:text="@string/cancel"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>

运行代码
在这里插入图片描述
3.项目清单文件AndroidManifest.xml
1.删除MainActivity元素的意图过滤器
在这里插入图片描述
2.登录窗口中声明变量,并写出按钮事件处理和取消登录按钮事件处理。
简单的监听事件有:
在这里插入图片描述
这次我们用到的是监听点击事件
多个的按钮的情况下就绑定多个onClickListener,每一个按钮对应一个处理代码。
最后onClick的参数View v,这个变量所指的是来自父层的ContentView,所以通过v.*可以更改其父层的View状态或属性

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private EditText edtUsername;
    private EditText edtpassword;
    private Button  btnLogin;
    private Button btnCancel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        edtUsername =findViewById(R.id.edtUsername);
        edtpassword =findViewById(R.id.edtPassword);
        btnLogin =  findViewById(R.id.btnLogin);
        btnCancel =findViewById(R.id.btnCancel);
        btnLogin.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String strUsername =edtUsername.getText().toString().trim();
                String strPassword =edtpassword.getText().toString().trim();

                if(strUsername.equals("admin")&& strPassword.equals("admin")){
                    Toast.makeText(LoginActivity.this,"恭喜,用户名与密码成功",Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(LoginActivity.this,"用户名或者密码错误",Toast.LENGTH_SHORT).show();

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

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!很高兴回答你关于Android Studio学习的问题。作为一个集成开发环境(IDE),Android Studio是开发Android应用程序的首选工具。下面是一些学习Android Studio的步骤和资源: 1. 下载和安装:首先,你需要从官方网站(https://developer.android.com/studio)下载并安装Android Studio。安装完成后,打开它并进行基本设置。 2. 学习界面:熟悉Android Studio的界面布局是重要的。它包含了项目结构、编辑器、日志等窗口。你可以通过官方文档和在线教程来了解这些界面元素的功能。 3. 学习Gradle构建系统:Gradle是Android Studio的构建系统,用于管理依赖项和构建过程。了解如何配置和使用Gradle将有助于你构建和管理项目。 4. 学习调试和测试:掌握调试和测试是开发高质量应用程序的关键。Android Studio提供了强大的调试工具,如断点调试和日志输出。你可以通过使用模拟器或连接设备来测试应用程序。 5. 学习Android开发:除了学习Android Studio本身,你还需要学习Android开发的基础知识,如布局、活动、片段、存储等。官方文档和在线教程是学习这些概念的好资源。 6. 社区支持和资源:加入Android开发者社区,参与讨论和交流经验。在网上有许多博客、论坛和视频教程可供参考。你还可以在GitHub上找到许多开源项目,可以学习和借鉴。 希望这些步骤和资源能帮助你开始学习Android Studio。祝你学习愉快,有任何问题都可以继续向我提问!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值