赞
踩
2.1 MainActivity.java完整代码
- package com.example.myapplication;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RadioButton;
-
- import androidx.activity.EdgeToEdge;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.graphics.Insets;
- import androidx.core.view.ViewCompat;
- import androidx.core.view.WindowInsetsCompat;
-
- public class MainActivity extends AppCompatActivity {
-
- RadioButton radioButton,radioButton2;
- Button button;
- EditText editTextText;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- EdgeToEdge.enable(this);
- setContentView(R.layout.activity_main);
-
-
- radioButton = findViewById(R.id.radioButton);
- radioButton2 = findViewById(R.id.radioButton2);
- button = findViewById(R.id.button);
- editTextText = findViewById(R.id.editTextText);
-
-
- //点击计算按钮跳转页面MainActivity2
- Button button = findViewById(R.id.button);
- button.setOnClickListener(v -> calculateWeight());
-
-
- ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
- Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
- v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
- return insets;
- });
- }
-
- private void calculateWeight() {
- String radioButtonValue = radioButton.getText().toString();
- int height = Integer.parseInt(editTextText.getText().toString());
- double weight;
- if (radioButton.isChecked()) {
- weight = (height - 80) * 0.7;
- } else {
- weight = (height - 80) * 0.6;
- radioButtonValue = "女";
- }
- Intent intent = new Intent(MainActivity.this, MainActivity2.class);
- //传递身高、体重数据到MainActivity2.class
- intent.putExtra("radioButtonValue",radioButtonValue);
- intent.putExtra("height", height);
- intent.putExtra("weight", weight);
- startActivity(intent);
- }
- }
2.2 activity_main.xml完整代码
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
-
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginEnd="160dp"
- android:layout_marginBottom="268dp"
- android:text="计算"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent" />
-
- <RadioButton
- android:id="@+id/radioButton2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="260dp"
- android:layout_marginTop="220dp"
- android:text="女"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <TextView
- android:id="@+id/textView3"
- android:layout_width="34dp"
- android:layout_height="0dp"
- android:layout_marginStart="100dp"
- android:layout_marginTop="352dp"
- android:text="身高"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello World!"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.479"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.95" />
-
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="156dp"
- android:layout_marginTop="132dp"
- android:text="计算你的标准体重!"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="100dp"
- android:layout_marginTop="232dp"
- android:text="性别"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <EditText
- android:id="@+id/editTextText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="340dp"
- android:layout_marginEnd="40dp"
- android:ems="10"
- android:inputType="text"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toEndOf="@+id/textView3"
- app:layout_constraintTop_toTopOf="parent" />
-
- <RadioButton
- android:id="@+id/radioButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="168dp"
- android:layout_marginTop="220dp"
- android:text="男"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- </androidx.constraintlayout.widget.ConstraintLayout>
2.3 MainActivity2.java完整代码
- package com.example.myapplication;
-
- import android.annotation.SuppressLint;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
-
- import androidx.activity.EdgeToEdge;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.graphics.Insets;
- import androidx.core.view.ViewCompat;
- import androidx.core.view.WindowInsetsCompat;
-
- public class MainActivity2 extends AppCompatActivity {
-
- TextView textView1;
- TextView textView2;
- TextView textView3;
-
-
-
- @SuppressLint("SetTextI18n")
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- EdgeToEdge.enable(this);
- setContentView(R.layout.activity_main2);
-
- textView1 = findViewById(R.id.textView1);
- textView2 = findViewById(R.id.textView2);
- textView3 = findViewById(R.id.textView3);
- Intent intent = getIntent();
-
- String radioButtonValue = intent.getStringExtra("radioButtonValue");
- double weight = intent.getDoubleExtra("weight", 0.0);
- int height = intent.getIntExtra("height",0);
- textView1.setText("你是一位 : " + radioButtonValue + " 性");
- textView2.setText("你的身高是 : " + height + " cm");
- textView3.setText("你的标准体重是 : " + weight + " kg");
-
- //点击按钮跳转到ListView的使用页面
- Button button = findViewById(R.id.jump);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent1 = new Intent(MainActivity2.this, MainActivity3.class);
- startActivity(intent1);
-
- }
- });
-
- ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
- Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
- v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
- return insets;
- });
- }
-
- }
2.4 activity_main2.xml完整代码
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity2">
-
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="124dp"
- android:layout_marginTop="112dp"
- android:text="你是一位: 性"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="124dp"
- android:layout_marginTop="172dp"
- android:text="你的身高是: 厘米"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <TextView
- android:id="@+id/textView3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="124dp"
- android:layout_marginTop="240dp"
- android:text="你的标准体重是: 公斤"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <Button
- android:id="@+id/jump"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="124dp"
- android:layout_marginTop="400dp"
- android:text="跳转到ListView页面"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
2.1 MainActivity3.java完整代码
- package com.example.myapplication;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
-
- import androidx.activity.EdgeToEdge;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.graphics.Insets;
- import androidx.core.view.ViewCompat;
- import androidx.core.view.WindowInsetsCompat;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class MainActivity3 extends AppCompatActivity {
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- EdgeToEdge.enable(this);
- setContentView(R.layout.activity_main3);
-
-
- //点击按钮跳转到ListView的使用页面
- Button button = findViewById(R.id.jump2);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent1 = new Intent(MainActivity3.this, MainActivity4.class);
- startActivity(intent1);
-
- }
- });
-
- final TextView textView = (TextView)findViewById(R.id.textView6);
- //ListView子项的信息
- ListView listView = (ListView)findViewById(R.id.Listview01);
- List<String>list = new ArrayList<String>();
- list.add("安卓应用开发基础-01");
- list.add("安卓应用开发基础-02");
- list.add("安卓应用开发基础-03");
- list.add("安卓应用开发基础-04");
- list.add("安卓应用开发基础-05");
- list.add("安卓应用开发基础-06");
- list.add("安卓应用开发基础-07");
- ArrayAdapter<String>adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
- listView.setAdapter(adapter);
-
- //ListView子项的点击监听器
- AdapterView.OnItemClickListener listViewListener = new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
- // String msg = "父 View: " + arg0.toString() + "\n"+"子 View: " +arg1.toString()+"\n"+"位置: "+String.valueOf(arg2)+",ID:"+String.valueOf(arg3);
- // 获取点击的子项对应的内容
- String msg = "您现在点击的是:"+ (String) arg0.getItemAtPosition(arg2);
- //Toast弹窗结果
- showToast("您点击了"+ (String) arg0.getItemAtPosition(arg2));
- //文本显示结果
- //textView.setText(msg);
- }
- };
- listView.setOnItemClickListener(listViewListener);
-
- //ListView子项长按监听器
- AdapterView.OnItemLongClickListener listViewLongClickListener = new AdapterView.OnItemLongClickListener(){
- @Override
- public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
- // 获取长按的子项对应的内容
- String msg = "您长按了:"+ (String) arg0.getItemAtPosition(arg2);
- // 弹出另一个气泡提示框
- showToast("您长按了"+ (String) arg0.getItemAtPosition(arg2));
- return true;
- }
- };
- listView.setOnItemLongClickListener(listViewLongClickListener);
-
-
-
- ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
- Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
- v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
- return insets;
- });
- }
-
- private void showToast(String message) {
- Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
- }
- }
2.2 activity_main3.xml完整代码
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity3">
-
- <Button
- android:id="@+id/jump2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="128dp"
- android:layout_marginTop="588dp"
- android:text="跳转到XML定义菜单"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <TextView
- android:id="@+id/textView5"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="156dp"
- android:layout_marginTop="56dp"
- android:text="ListView的使用"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
- <ListView
- android:id="@+id/Listview01"
- android:layout_width="409dp"
- android:layout_height="437dp"
- android:layout_marginTop="128dp"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent">
-
- </ListView>
-
- <TextView
- android:id="@+id/textView6"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="96dp"
- android:layout_marginEnd="104dp"
- android:text="hello!"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
-
- </androidx.constraintlayout.widget.ConstraintLayout>
2.1 MainActivity4.java完整代码
- package com.example.myapplication;
-
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.widget.TextView;
- import android.widget.Toast;
-
- import androidx.activity.EdgeToEdge;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.graphics.Insets;
- import androidx.core.view.ViewCompat;
- import androidx.core.view.WindowInsetsCompat;
-
- public class MainActivity4 extends AppCompatActivity {
-
- Toast toast;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- EdgeToEdge.enable(this);
-
- setContentView(R.layout.activity_main4);
-
- ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
- Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
- v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
- return insets;
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu){
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.menu_main,menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item){
- TextView label = (TextView) findViewById(R.id.label);
-
- int itemId = item.getItemId();
- if (itemId == R.id.main_menu_01) {
- label.setTextSize(10);
- return true;
- } else if (itemId == R.id.main_menu_02) {
- label.setTextSize(16);
- return true;
- } else if (itemId == R.id.main_menu_03) {
- label.setTextSize(20);
- return true;
- } else if (itemId == R.id.main_menu_12) {
- label.setTextColor(Color.RED);
- return true;
- } else if (itemId == R.id.main_menu_11) {
- label.setTextColor(Color.BLACK);
- return true;
- } else if (itemId == R.id.main_menu_2) {
- showToast("您点击了基本菜单项");
- return true;
- }
- else {
- return false;
- }
-
- }
- private void showToast(String message) {
- Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
- }
- }
2.2 activity_main4.xml完整代码
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity4">
-
- <TextView
- android:id="@+id/label"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="128dp"
- android:layout_marginTop="488dp"
- android:text="XML定义菜单哈哈哈哈哈"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
-
-
- </androidx.constraintlayout.widget.ConstraintLayout>
2.3 menu_main.xml完整代码
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
-
- <item android:id="@+id/main_menu_0"
- android:icon="@drawable/ic_launcher_background"
- android:title="字体大小">
- <menu>
- <item android:id="@+id/main_menu_01"
- android:title="小号:10"/>
-
- <item android:id="@+id/main_menu_02"
- android:title="中号:16"/>
-
- <item android:id="@+id/main_menu_03"
- android:title="大号:20"/>
- </menu>
- </item>
- <item android:id="@+id/main_menu_1"
- android:icon="@drawable/ic_launcher_background"
- android:title="字体颜色">
- <menu>
- <item android:id="@+id/main_menu_11"
- android:title="黑色"/>
-
- <item android:id="@+id/main_menu_12"
- android:title="红色"/>
- </menu>
- </item>
-
- <item android:id="@+id/main_menu_2"
- android:icon="@drawable/ic_launcher_background"
- android:title="普通菜单项"/>
- <item android:id="@+id/main_menu_3"
- android:icon="@drawable/ic_launcher_background"
- android:title="设置"/>
- <item android:id="@+id/main_menu_4"
- android:icon="@drawable/ic_launcher_background"
- android:title="订阅"/>
-
-
- </menu>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。