当前位置:   article > 正文

安卓技术实战01 第一个安卓APP案例_public class mainactivity extends appcompata代码注入到哪

public class mainactivity extends appcompata代码注入到哪个文件里面

目录

1. 创建项目与文件组织

 2 线性布局与控件

2.1 文本框

2.2 嵌套线性布局

2.3 为控件自定义背景图片

3 页面跳转

3.1 直接跳转方法

3.2 编写单独的点击函数

3.3 Toast提示


编译器选择:Android Studio

1. 创建项目与文件组织

创建一个新的 Empty Activity,设置项目名称,选择项目路径及编程语言;点击 Finish 即可。

 项目创建完成后,先将项目视图切换为Project模式

这里重点关注src组织下的内容:

  1. MainActivity下,写应用程序相关的java代码
  2. layout下存储的是布局文件,我们可以在布局文件中添加需要的控件
  3. values下存放的是颜色、字符串、风格样式等信息
  4. Manifest下指定我们要启动的activity
  5. drawable下存放的是程序需要的背景文件

 2 线性布局与控件

下面我们将重心转回到activity_main.xml,学习线性布局,重点关注以以下几个要点:

  • 线性布局中必须要包含的一个属性是 android:orientation="vertical"
  • 布局的宽度和高度有 match_parent 和 wrap_content 两种模式,前者表示匹配布局;后者则是根据内容自适应
  • 设置背景图片
    • 首先设计好自己需要的背景素材,将其拷贝到drawable目录下,并修改成你需要的名字
    • 在activity_main的线性布局中,android:background="@drawable/app_background"
  • 线性布局支持嵌套

 我们可以在线性布局中定义相关的控件:

2.1 文本框

  1. <TextView
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:id="@+id/tv_username"
  5. android:hint="用户名"
  6. android:textColorHint="#FFFFFF"
  7. android:textColor="#FFFFFF"
  8. android:textSize="30sp"/>

 必须定义的属性字段:

  • id:后续使用该控件的唯一标识
  • hint:指定文本控件的提示文本
  • layout_width:控件的宽度
  • layout_height:控件的高度
  • textSize:文本的尺寸
  • textColor:文本的颜色

可选字段:

  • 密码密文显示:android:inputType="textPassword"
  • 文本与边框的距离:android:padding="10dp"
  • 提示文本的颜色设定:android:textColorHint="#FFFFFF"

2.2 嵌套线性布局

案例:在一个线性布局上,放置两个登录和注册按钮

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="horizontal"
  5. >
  6. <Button
  7. android:layout_width="0dp"
  8. android:layout_height="wrap_content"
  9. android:layout_weight="1"
  10. android:text="登录"
  11. android:id="@+id/btn_login"
  12. />
  13. <Button
  14. android:layout_width="0dp"
  15. android:layout_height="wrap_content"
  16. android:layout_weight="1"
  17. android:text="注册"
  18. android:id="@+id/btn_register"></Button>
  19. </LinearLayout>

2.3 为控件自定义背景图片

自定义背景文件

 登录按钮的背景文件定义代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:shape="rectangle">
  5. <corners
  6. android:topLeftRadius="5dp"
  7. android:bottomLeftRadius="5dp"/>
  8. <stroke
  9. android:width="1dp"
  10. android:color="#ffffff"
  11. android:dashGap="1dp"/>
  12. </shape>

下面是完整的线性布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. android:background="@drawable/app_background"
  9. tools:context=".MainActivity">
  10. <TextView
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:id="@+id/tv_username"
  14. android:hint="用户名"
  15. android:textColorHint="#FFFFFF"
  16. android:textColor="#FFFFFF"
  17. android:textSize="30sp"
  18. android:padding="10dp"/>
  19. <TextView
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:id="@+id/tv_password"
  23. android:hint="密码"
  24. android:textColorHint="#FFFFFF"
  25. android:textColor="#FFFFFF"
  26. android:textSize="30sp"
  27. android:padding="10dp"
  28. android:inputType="textPassword"
  29. />
  30. <LinearLayout
  31. android:layout_width="match_parent"
  32. android:layout_height="match_parent"
  33. android:orientation="horizontal"
  34. >
  35. <Button
  36. android:layout_width="0dp"
  37. android:layout_height="wrap_content"
  38. android:layout_weight="1"
  39. android:text="登录"
  40. android:textColor="#FFFFFF"
  41. android:id="@+id/btn_login"
  42. android:background="@drawable/bg_btnlogin"
  43. />
  44. <Button
  45. android:layout_width="0dp"
  46. android:layout_height="wrap_content"
  47. android:layout_weight="1"
  48. android:text="注册"
  49. android:textColor="#FFFFFF"
  50. android:id="@+id/btn_register"
  51. android:background="@drawable/bg_btnregister"></Button>
  52. </LinearLayout>
  53. </LinearLayout>

效果如下:

3 页面跳转

3.1 直接跳转方法

方法1:在src -- main -- java下的MainActivity.java的同一层次下,创建一个新的Empty Activity,

设置完名字后,直接点击Finish。

 可以看到,创建Activity的同时也创建了相应的布局文件。

同样地,我们在布局文件中,简单添加一个TextView,提示我们跳转成功了。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. android:padding="10dp"
  9. tools:context=".Main2Activity">
  10. <TextView
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:id="@+id/tv_tiaozhuan"
  14. android:text="跳转之后的网页"
  15. android:gravity="center"></TextView>
  16. </LinearLayout>

 然后,在主Activity中的java文件中,添加跳转的代码:

  • 首先创建按钮类型的成员变量
  • 通过findViewById方法找到按钮,这个id就是前面我们在布局文件中写的id了
  • 添加按钮的点击事件,主要是重写onClick()方法,新建intent并启动intent
  • intent的参数中,第一个是主Activity.this, 第二个是跳转Activity.class.
  1. package com.example.myapplicationexample;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. public class MainActivity extends AppCompatActivity {
  8. private Button mBtnLogin;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. // 根据id找到控件
  14. mBtnLogin = findViewById(R.id.btn_login);
  15. // 实现跳转
  16. mBtnLogin.setOnClickListener(new View.OnClickListener() {
  17. @Override
  18. public void onClick(View v) {
  19. Intent intent = null;
  20. intent = new Intent(MainActivity.this, Main2Activity.class);
  21. startActivity(intent);
  22. }
  23. });
  24. }
  25. }

3.2 编写单独的点击函数

  • 点击函数编写
  1. public void onClick(View view) {
  2. String username = mEtUsername.getText().toString();
  3. String password = mEtPassword.getText().toString();
  4. Intent intent = null;
  5. if(username.equals("myz") && password.equals("gis2021"))
  6. {
  7. ToastUtil.showMessage(MainActivity.this, "账号密码正确");
  8. intent = new Intent(MainActivity.this, Main2Activity.class);
  9. startActivity(intent);
  10. }else {
  11. }
  12. }
  • 调用函数编写,这里需要使用 alt+enter 进行类集成
  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2. private Button mBtnLogin;
  3. private EditText mEtUsername;
  4. private EditText mEtPassword;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. // 根据id找到控件
  10. mBtnLogin = findViewById(R.id.btn_login);
  11. mEtPassword = findViewById(R.id.et_password);
  12. mEtUsername = findViewById(R.id.et_username);
  13. // alt + enter查看错误
  14. mBtnLogin.setOnClickListener(this);
  15. }
  16. public void onClick(View view) {
  17. String username = mEtUsername.getText().toString();
  18. String password = mEtPassword.getText().toString();
  19. Intent intent = null;
  20. if(username.equals("myz") && password.equals("gis2021"))
  21. {
  22. intent = new Intent(MainActivity.this, Main2Activity.class);
  23. startActivity(intent);
  24. }else {
  25. }
  26. }
  27. @Override
  28. public void onPointerCaptureChanged(boolean hasCapture) {
  29. }
  30. }

3.3 Toast提示

方法1:直接在底部显示:

Toast.makeText(getApplicationContext(), "账号密码正确", Toast.LENGTH_SHORT).show();

方法2:居中弹出:

  1. Toast centerToast = Toast.makeText(getApplicationContext(), "账号或密码错误", Toast.LENGTH_SHORT);
  2. centerToast.setGravity(Gravity.CENTER, 0,0);
  3. centerToast.show();

方法3:定义类弹出

  • 创建包

  •  在包中新建java类

 

  • 撰写java代码,定义类文件:
  1. package com.example.myapplicationexample.util;
  2. import android.content.Context;
  3. import android.widget.Toast;
  4. public class ToastUtil {
  5. public static Toast mToast;
  6. public static void showMessage(Context context, String wsg)
  7. {
  8. if(mToast == null)
  9. {
  10. mToast = Toast.makeText(context, wsg, Toast.LENGTH_SHORT);
  11. }else{
  12. mToast.setText(wsg);
  13. }
  14. mToast.show();
  15. }
  16. }
  •  类调用:
ToastUtil.showMessage(MainActivity.this, "账号密码正确");

相关代码链接:

MyApplicationExample.zip-其它文档类资源-CSDN下载

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号