当前位置:   article > 正文

Android 应用程序开发实验(二)熟悉安卓常见的UI组件_viewcompat.setonapplywindowinsetslistener(findview

viewcompat.setonapplywindowinsetslistener(findviewbyid(r.id.main), (v, inset

(1)基本常用组件

        ①布局预览:

        ②代码设计:

2.1   MainActivity.java完整代码

  1. package com.example.myapplication;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import android.widget.RadioButton;
  9. import androidx.activity.EdgeToEdge;
  10. import androidx.appcompat.app.AppCompatActivity;
  11. import androidx.core.graphics.Insets;
  12. import androidx.core.view.ViewCompat;
  13. import androidx.core.view.WindowInsetsCompat;
  14. public class MainActivity extends AppCompatActivity {
  15. RadioButton radioButton,radioButton2;
  16. Button button;
  17. EditText editTextText;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. EdgeToEdge.enable(this);
  22. setContentView(R.layout.activity_main);
  23. radioButton = findViewById(R.id.radioButton);
  24. radioButton2 = findViewById(R.id.radioButton2);
  25. button = findViewById(R.id.button);
  26. editTextText = findViewById(R.id.editTextText);
  27. //点击计算按钮跳转页面MainActivity2
  28. Button button = findViewById(R.id.button);
  29. button.setOnClickListener(v -> calculateWeight());
  30. ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
  31. Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
  32. v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
  33. return insets;
  34. });
  35. }
  36. private void calculateWeight() {
  37. String radioButtonValue = radioButton.getText().toString();
  38. int height = Integer.parseInt(editTextText.getText().toString());
  39. double weight;
  40. if (radioButton.isChecked()) {
  41. weight = (height - 80) * 0.7;
  42. } else {
  43. weight = (height - 80) * 0.6;
  44. radioButtonValue = "女";
  45. }
  46. Intent intent = new Intent(MainActivity.this, MainActivity2.class);
  47. //传递身高、体重数据到MainActivity2.class
  48. intent.putExtra("radioButtonValue",radioButtonValue);
  49. intent.putExtra("height", height);
  50. intent.putExtra("weight", weight);
  51. startActivity(intent);
  52. }
  53. }

2.2   activity_main.xml完整代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9. <Button
  10. android:id="@+id/button"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginEnd="160dp"
  14. android:layout_marginBottom="268dp"
  15. android:text="计算"
  16. app:layout_constraintBottom_toBottomOf="parent"
  17. app:layout_constraintEnd_toEndOf="parent" />
  18. <RadioButton
  19. android:id="@+id/radioButton2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_marginStart="260dp"
  23. android:layout_marginTop="220dp"
  24. android:text="女"
  25. app:layout_constraintStart_toStartOf="parent"
  26. app:layout_constraintTop_toTopOf="parent" />
  27. <TextView
  28. android:id="@+id/textView3"
  29. android:layout_width="34dp"
  30. android:layout_height="0dp"
  31. android:layout_marginStart="100dp"
  32. android:layout_marginTop="352dp"
  33. android:text="身高"
  34. app:layout_constraintStart_toStartOf="parent"
  35. app:layout_constraintTop_toTopOf="parent" />
  36. <TextView
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:text="Hello World!"
  40. app:layout_constraintBottom_toBottomOf="parent"
  41. app:layout_constraintEnd_toEndOf="parent"
  42. app:layout_constraintHorizontal_bias="0.479"
  43. app:layout_constraintStart_toStartOf="parent"
  44. app:layout_constraintTop_toTopOf="parent"
  45. app:layout_constraintVertical_bias="0.95" />
  46. <TextView
  47. android:id="@+id/textView"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:layout_marginStart="156dp"
  51. android:layout_marginTop="132dp"
  52. android:text="计算你的标准体重!"
  53. app:layout_constraintStart_toStartOf="parent"
  54. app:layout_constraintTop_toTopOf="parent" />
  55. <TextView
  56. android:id="@+id/textView2"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:layout_marginStart="100dp"
  60. android:layout_marginTop="232dp"
  61. android:text="性别"
  62. app:layout_constraintStart_toStartOf="parent"
  63. app:layout_constraintTop_toTopOf="parent" />
  64. <EditText
  65. android:id="@+id/editTextText"
  66. android:layout_width="wrap_content"
  67. android:layout_height="wrap_content"
  68. android:layout_marginTop="340dp"
  69. android:layout_marginEnd="40dp"
  70. android:ems="10"
  71. android:inputType="text"
  72. app:layout_constraintEnd_toEndOf="parent"
  73. app:layout_constraintStart_toEndOf="@+id/textView3"
  74. app:layout_constraintTop_toTopOf="parent" />
  75. <RadioButton
  76. android:id="@+id/radioButton"
  77. android:layout_width="wrap_content"
  78. android:layout_height="wrap_content"
  79. android:layout_marginStart="168dp"
  80. android:layout_marginTop="220dp"
  81. android:text="男"
  82. app:layout_constraintStart_toStartOf="parent"
  83. app:layout_constraintTop_toTopOf="parent" />
  84. </androidx.constraintlayout.widget.ConstraintLayout>

2.3   MainActivity2.java完整代码

  1. package com.example.myapplication;
  2. import android.annotation.SuppressLint;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. import androidx.activity.EdgeToEdge;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import androidx.core.graphics.Insets;
  11. import androidx.core.view.ViewCompat;
  12. import androidx.core.view.WindowInsetsCompat;
  13. public class MainActivity2 extends AppCompatActivity {
  14. TextView textView1;
  15. TextView textView2;
  16. TextView textView3;
  17. @SuppressLint("SetTextI18n")
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. EdgeToEdge.enable(this);
  22. setContentView(R.layout.activity_main2);
  23. textView1 = findViewById(R.id.textView1);
  24. textView2 = findViewById(R.id.textView2);
  25. textView3 = findViewById(R.id.textView3);
  26. Intent intent = getIntent();
  27. String radioButtonValue = intent.getStringExtra("radioButtonValue");
  28. double weight = intent.getDoubleExtra("weight", 0.0);
  29. int height = intent.getIntExtra("height",0);
  30. textView1.setText("你是一位 : " + radioButtonValue + " 性");
  31. textView2.setText("你的身高是 : " + height + " cm");
  32. textView3.setText("你的标准体重是 : " + weight + " kg");
  33. //点击按钮跳转到ListView的使用页面
  34. Button button = findViewById(R.id.jump);
  35. button.setOnClickListener(new View.OnClickListener() {
  36. @Override
  37. public void onClick(View v) {
  38. Intent intent1 = new Intent(MainActivity2.this, MainActivity3.class);
  39. startActivity(intent1);
  40. }
  41. });
  42. ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
  43. Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
  44. v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
  45. return insets;
  46. });
  47. }
  48. }

2.4   activity_main2.xml完整代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity2">
  9. <TextView
  10. android:id="@+id/textView1"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginStart="124dp"
  14. android:layout_marginTop="112dp"
  15. android:text="你是一位: 性"
  16. app:layout_constraintStart_toStartOf="parent"
  17. app:layout_constraintTop_toTopOf="parent" />
  18. <TextView
  19. android:id="@+id/textView2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_marginStart="124dp"
  23. android:layout_marginTop="172dp"
  24. android:text="你的身高是: 厘米"
  25. app:layout_constraintStart_toStartOf="parent"
  26. app:layout_constraintTop_toTopOf="parent" />
  27. <TextView
  28. android:id="@+id/textView3"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:layout_marginStart="124dp"
  32. android:layout_marginTop="240dp"
  33. android:text="你的标准体重是: 公斤"
  34. app:layout_constraintStart_toStartOf="parent"
  35. app:layout_constraintTop_toTopOf="parent" />
  36. <Button
  37. android:id="@+id/jump"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:layout_marginStart="124dp"
  41. android:layout_marginTop="400dp"
  42. android:text="跳转到ListView页面"
  43. app:layout_constraintStart_toStartOf="parent"
  44. app:layout_constraintTop_toTopOf="parent" />
  45. </androidx.constraintlayout.widget.ConstraintLayout>

(2)ListView的使用

        ①布局预览:

        ②代码设计:

2.1   MainActivity3.java完整代码

  1. package com.example.myapplication;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.AdapterView;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.Button;
  8. import android.widget.ListView;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11. import androidx.activity.EdgeToEdge;
  12. import androidx.appcompat.app.AppCompatActivity;
  13. import androidx.core.graphics.Insets;
  14. import androidx.core.view.ViewCompat;
  15. import androidx.core.view.WindowInsetsCompat;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. public class MainActivity3 extends AppCompatActivity {
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. EdgeToEdge.enable(this);
  23. setContentView(R.layout.activity_main3);
  24. //点击按钮跳转到ListView的使用页面
  25. Button button = findViewById(R.id.jump2);
  26. button.setOnClickListener(new View.OnClickListener() {
  27. @Override
  28. public void onClick(View v) {
  29. Intent intent1 = new Intent(MainActivity3.this, MainActivity4.class);
  30. startActivity(intent1);
  31. }
  32. });
  33. final TextView textView = (TextView)findViewById(R.id.textView6);
  34. //ListView子项的信息
  35. ListView listView = (ListView)findViewById(R.id.Listview01);
  36. List<String>list = new ArrayList<String>();
  37. list.add("安卓应用开发基础-01");
  38. list.add("安卓应用开发基础-02");
  39. list.add("安卓应用开发基础-03");
  40. list.add("安卓应用开发基础-04");
  41. list.add("安卓应用开发基础-05");
  42. list.add("安卓应用开发基础-06");
  43. list.add("安卓应用开发基础-07");
  44. ArrayAdapter<String>adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
  45. listView.setAdapter(adapter);
  46. //ListView子项的点击监听器
  47. AdapterView.OnItemClickListener listViewListener = new AdapterView.OnItemClickListener() {
  48. @Override
  49. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  50. // String msg = "父 View: " + arg0.toString() + "\n"+"子 View: " +arg1.toString()+"\n"+"位置: "+String.valueOf(arg2)+",ID:"+String.valueOf(arg3);
  51. // 获取点击的子项对应的内容
  52. String msg = "您现在点击的是:"+ (String) arg0.getItemAtPosition(arg2);
  53. //Toast弹窗结果
  54. showToast("您点击了"+ (String) arg0.getItemAtPosition(arg2));
  55. //文本显示结果
  56. //textView.setText(msg);
  57. }
  58. };
  59. listView.setOnItemClickListener(listViewListener);
  60. //ListView子项长按监听器
  61. AdapterView.OnItemLongClickListener listViewLongClickListener = new AdapterView.OnItemLongClickListener(){
  62. @Override
  63. public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  64. // 获取长按的子项对应的内容
  65. String msg = "您长按了:"+ (String) arg0.getItemAtPosition(arg2);
  66. // 弹出另一个气泡提示框
  67. showToast("您长按了"+ (String) arg0.getItemAtPosition(arg2));
  68. return true;
  69. }
  70. };
  71. listView.setOnItemLongClickListener(listViewLongClickListener);
  72. ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
  73. Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
  74. v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
  75. return insets;
  76. });
  77. }
  78. private void showToast(String message) {
  79. Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
  80. }
  81. }

2.2   activity_main3.xml完整代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity3">
  9. <Button
  10. android:id="@+id/jump2"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginStart="128dp"
  14. android:layout_marginTop="588dp"
  15. android:text="跳转到XML定义菜单"
  16. app:layout_constraintStart_toStartOf="parent"
  17. app:layout_constraintTop_toTopOf="parent" />
  18. <TextView
  19. android:id="@+id/textView5"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_marginStart="156dp"
  23. android:layout_marginTop="56dp"
  24. android:text="ListView的使用"
  25. app:layout_constraintStart_toStartOf="parent"
  26. app:layout_constraintTop_toTopOf="parent" />
  27. <ListView
  28. android:id="@+id/Listview01"
  29. android:layout_width="409dp"
  30. android:layout_height="437dp"
  31. android:layout_marginTop="128dp"
  32. app:layout_constraintStart_toStartOf="parent"
  33. app:layout_constraintTop_toTopOf="parent">
  34. </ListView>
  35. <TextView
  36. android:id="@+id/textView6"
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:layout_marginTop="96dp"
  40. android:layout_marginEnd="104dp"
  41. android:text="hello!"
  42. app:layout_constraintEnd_toEndOf="parent"
  43. app:layout_constraintTop_toTopOf="parent" />
  44. </androidx.constraintlayout.widget.ConstraintLayout>

(3)XML定义菜单

        ①布局预览:

        ②代码设计:

2.1   MainActivity4.java完整代码

  1. package com.example.myapplication;
  2. import android.graphics.Color;
  3. import android.os.Bundle;
  4. import android.view.Gravity;
  5. import android.view.Menu;
  6. import android.view.MenuInflater;
  7. import android.view.MenuItem;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. import androidx.activity.EdgeToEdge;
  11. import androidx.appcompat.app.AppCompatActivity;
  12. import androidx.core.graphics.Insets;
  13. import androidx.core.view.ViewCompat;
  14. import androidx.core.view.WindowInsetsCompat;
  15. public class MainActivity4 extends AppCompatActivity {
  16. Toast toast;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. EdgeToEdge.enable(this);
  21. setContentView(R.layout.activity_main4);
  22. ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
  23. Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
  24. v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
  25. return insets;
  26. });
  27. }
  28. @Override
  29. public boolean onCreateOptionsMenu(Menu menu){
  30. MenuInflater inflater = getMenuInflater();
  31. inflater.inflate(R.menu.menu_main,menu);
  32. return true;
  33. }
  34. @Override
  35. public boolean onOptionsItemSelected(MenuItem item){
  36. TextView label = (TextView) findViewById(R.id.label);
  37. int itemId = item.getItemId();
  38. if (itemId == R.id.main_menu_01) {
  39. label.setTextSize(10);
  40. return true;
  41. } else if (itemId == R.id.main_menu_02) {
  42. label.setTextSize(16);
  43. return true;
  44. } else if (itemId == R.id.main_menu_03) {
  45. label.setTextSize(20);
  46. return true;
  47. } else if (itemId == R.id.main_menu_12) {
  48. label.setTextColor(Color.RED);
  49. return true;
  50. } else if (itemId == R.id.main_menu_11) {
  51. label.setTextColor(Color.BLACK);
  52. return true;
  53. } else if (itemId == R.id.main_menu_2) {
  54. showToast("您点击了基本菜单项");
  55. return true;
  56. }
  57. else {
  58. return false;
  59. }
  60. }
  61. private void showToast(String message) {
  62. Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
  63. }
  64. }

2.2   activity_main4.xml完整代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity4">
  9. <TextView
  10. android:id="@+id/label"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginStart="128dp"
  14. android:layout_marginTop="488dp"
  15. android:text="XML定义菜单哈哈哈哈哈"
  16. app:layout_constraintStart_toStartOf="parent"
  17. app:layout_constraintTop_toTopOf="parent" />
  18. </androidx.constraintlayout.widget.ConstraintLayout>

2.3   menu_main.xml完整代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:id="@+id/main_menu_0"
  4. android:icon="@drawable/ic_launcher_background"
  5. android:title="字体大小">
  6. <menu>
  7. <item android:id="@+id/main_menu_01"
  8. android:title="小号:10"/>
  9. <item android:id="@+id/main_menu_02"
  10. android:title="中号:16"/>
  11. <item android:id="@+id/main_menu_03"
  12. android:title="大号:20"/>
  13. </menu>
  14. </item>
  15. <item android:id="@+id/main_menu_1"
  16. android:icon="@drawable/ic_launcher_background"
  17. android:title="字体颜色">
  18. <menu>
  19. <item android:id="@+id/main_menu_11"
  20. android:title="黑色"/>
  21. <item android:id="@+id/main_menu_12"
  22. android:title="红色"/>
  23. </menu>
  24. </item>
  25. <item android:id="@+id/main_menu_2"
  26. android:icon="@drawable/ic_launcher_background"
  27. android:title="普通菜单项"/>
  28. <item android:id="@+id/main_menu_3"
  29. android:icon="@drawable/ic_launcher_background"
  30. android:title="设置"/>
  31. <item android:id="@+id/main_menu_4"
  32. android:icon="@drawable/ic_launcher_background"
  33. android:title="订阅"/>
  34. </menu>

(4)实验说明
           MainActivity2.java与MainActivity3.java中分别设置了跳转页面的代码,方便个人学习调试,不需要可选择删除跳转按钮以及相关代码。后续相关学习实验内容也会进行更新发布,感谢大家的喜欢与支持!作者学习Android 应用程序开发的过程中,如有不足之处,期待您的指正哦!

(5)寄语           

           学习之路漫漫长,薪资之望快快涨。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/387393
推荐阅读
相关标签
  

闽ICP备14008679号