zoukankan      html  css  js  c++  java
  • 10 Button控件

    Button控件:

    普通按钮;带图标的按钮;按钮事件

    MainActivity.java

    package com.example.helloworld;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btnBmiCalc = (Button)findViewById(R.id.btnBmiCalc);
            btnBmiCalc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v){
                    EditText editHeight = (EditText)findViewById(R.id.editHeight);
                    EditText editWeight = (EditText)findViewById(R.id.editWeight);
                    TextView txtResult = (TextView)findViewById(R.id.txtResult);
                    Double height = Double.parseDouble(editHeight.getText().toString());
                    Double weight = Double.parseDouble(editWeight.getText().toString());
                    Double bmi = weight/(height * height);
                    if(bmi<18.5){
                        txtResult.setText("BMI:"+bmi.toString() + "体型:过轻");
                    }
                    else if(bmi<=23.9){
                        txtResult.setText("BMI:"+bmi.toString() + "体型:正常");
                    }
                    else if(bmi<=27){
                        txtResult.setText("BMI:"+bmi.toString() + "体型:过重");
                    }
                    else if(bmi<=32){
                        txtResult.setText("BMI:"+bmi.toString() + "体型:肥胖");
                    }
                    else{
                        txtResult.setText("BMI:"+bmi.toString() + "体型:非常肥胖");
                    }
    
                }
            });
    
        }
    
    
    }

    activity_main.xml

    <?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"
        tools:context=".MainActivity" >
    
        <TextView
            android:id="@+id/textView6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/BmiTitle"
            android:textSize="24sp" />
    
        <EditText
            android:id="@+id/editHeight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/BodyHeight"
            android:inputType="numberDecimal" />
    
        <EditText
            android:id="@+id/editWeight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/BodyWeight"
            android:inputType="numberDecimal" />
    
        <TextView
            android:id="@+id/txtResult"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/BodyHeight"
            tools:text="@string/Result" />
    
        <Button
            android:id="@+id/btnBmiCalc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="BtnBmiCalc_Clicked"
            android:text="计算" />
    </LinearLayout>

    截图:

  • 相关阅读:
    什么是软件架构?
    子系统、框架与架构
    今天开始锻炼身体
    程序语言中基本数值类型的分类
    软件架构的作用
    软件架构要设计到什么程度
    软件架构视图
    更多资料
    How to:如何在调用外部文件时调试文件路径(常见于使用LaunchAppAndWait和LaunchApp函数)
    installshield卸载时提示重启动的原因以及解决办法
  • 原文地址:https://www.cnblogs.com/wangdayang/p/14397738.html
Copyright © 2011-2022 走看看