学习笔记--第三周

本周Android学习总结

activity生命周期

测试:
在MainActivity.java中写入如下代码

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "life_cycle";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate invoked. (创建)");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart invoked. (已开始)");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume invoked. (已恢复)");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause invoked. (暂停)");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop invoked. (已停止)");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart invoked. (已重启)");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy invoked. (释放资源)");
    }
}

点击左下角第四个按钮logcat,在最右边的下拉框中选择Edit Filter Configuration
在这里插入图片描述
选择按标签过滤并写入标签名,下面选择debug
在这里插入图片描述
在这里插入图片描述
生命周期这一块并没有完全弄懂,用户在恢复和暂停期间可以交互这些也很好理解,但是总觉得漏了点什么;这方面的应用倒是见过不少,比如某大砍刀购物软件,你帮别人砍刀之后再退出的时候总是有一堆弹窗跳出来,这应该是在调用关闭应用那几个回调方法的时候设置的吧(个人理解)。

窗口跳转(携带数据)

首先编写首页,注意在需要携带数据的地方和跳转按钮添加id属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="15sp"
    android:background="@drawable/bk012"
    tools:context=".LoginActivity">

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

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

        <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="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/input_username"
            android:ems="10"
            android:singleLine="true"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        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="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/input_password"
            android:inputType="textPassword"
            android:ems="10"
            android:singleLine="true"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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:background="@color/danlv"
            android:textColor="#ffffff"
            android:text="@string/login"
            android:layout_marginRight="10dp"
            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:background="@color/danlv"
            android:text="@string/cancel"
            android:textSize="20sp"
            android:textColor="#ffffff"/>
    </LinearLayout>
</LinearLayout>

之后编写对应的Java页面(注释很清楚)

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);
        //活动栏图标
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setLogo(R.mipmap.ic_launcher);
        //获取控件实例
        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 view) {
                //保存用户输入数据
                String strUsername=edtUsername.getText().toString().trim();//trim()去除用户在前面输入的空格
                String strPassword=edtPassword.getText().toString().trim();
                //判断用户名密码是否正确
                if (strUsername.equals("admin") && strPassword.equals("pwd")) {
                    //利用吐司提示用户登录情况
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();//LENGTH_SHORT表示时间长度
                    //创建显示意图
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    //创建数据包封装数据(封装)
                    Bundle date = new Bundle();
                    date.putString("username", strUsername);
                    date.putString("password", strPassword);
                    //通过意图携带数据包
                    intent.putExtras(date);
                    //按照意图启动目标组件
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, "用户名密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //关闭窗口
                finish();
            }
        });
    }
}

然后编写跳转后的页面(这里只简单弄个展示),还是注意添加id属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@drawable/bk016"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="25dp"
        android:textColor="#ff00ff"/>

</LinearLayout>

再之后编写跳转后页面对应的Java文件,用于接收数据

public class MainActivity extends AppCompatActivity {
    protected TextView tvMessage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //利用布局资源设置用户界面
        setContentView(R.layout.activity_main);
       //通过资源标识符获取控件实例
        tvMessage = findViewById(R.id.tvMessage);
        //获取意图
        Intent intent = getIntent();
        //判断意图是否为空
        if (intent != null) {
            //获取意图携带数据包(封装)
            Bundle date = intent.getExtras();
            String username = date.getString("username");
            String password = date.getString("password");
            //拼接用户信息
            String message = "登陆成功!\n用户:" + username + "\n密码:" + password;
            //设置标签属性,显示用户信息
            tvMessage.setText(message);
        }

    }
}

最后添加布局文件启动
在这里插入图片描述
效果
在这里插入图片描述

在这里插入图片描述
跳转这一块学起来真的很有意思(或许是因为可视化可变动的原因),这一块感觉并不是难主要是繁琐;上面只说了封装,主要是我有一种思维(或许大多数人都有)–数据封装更专业,更安全。封装的逻辑也很清晰。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值