当前位置: 移动技术网 > IT编程>移动开发>Android > Android Studio实现长方体表面积计算器

Android Studio实现长方体表面积计算器

2020年06月18日  | 移动技术网IT编程  | 我要评论

赵贤伟,广州军区历任司令员,北京车辆限号查询

本文实例为大家分享了android studio实现长方体、表面积计算器的具体代码,供大家参考,具体内容如下

写了两个方法(在我理解之中有三个方法,其中循环字符串匹配太low了,pass掉),目前先上传一个,后续补上。
针对这个问题 总共有四个文件

方法一:正则表达式

1.mainactivity.java

package com.example.flyyu.four;

import android.content.intent;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.edittext;
import android.widget.toast;

import java.util.regex.pattern;

public class mainactivity extends appcompatactivity implements view.onclicklistener{

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 }
 @override
 public void onclick(view v) {
 edittext a=(edittext)findviewbyid(r.id.edittext);

 edittext b=(edittext)findviewbyid(r.id.edittext2);

 edittext c=(edittext)findviewbyid(r.id.edittext3);
 intent it=new intent(this,biapmianji.class);
 if (!isnumber(a.gettext().tostring())&&!isnumber(b.gettext().tostring())&&!isnumber(c.gettext().tostring())){
 it.putextra("a",a.gettext().tostring());
 it.putextra("b",b.gettext().tostring());
 it.putextra("c",c.gettext().tostring());
 mainactivity.this.startactivity(it);
 }
 }
 public boolean isnumber(string s){

 string pattern = ".*\\d.*";

 boolean ismatch = pattern.matches(pattern,s);
 if (ismatch||s.length()==0){
 toast.maketext(this,"输入异常",toast.length_short).show();
 return true;
 }
 return ismatch;
 }
}

2.biapmianji.java

package com.example.flyyu.four;

import android.content.intent;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.textview;

public class biapmianji extends appcompatactivity {

 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_biapmianji);
 textview textview=(textview)findviewbyid(r.id.textview4);
 intent it=this.getintent();
 float a=float.valueof(it.getstringextra("a")) ;
 float b=float.valueof(it.getstringextra("b")) ;
 float c=float.valueof(it.getstringextra("c")) ;
 string s=(2*(a*b+a*c+b*c))+"";
 textview.settext("该长方体的表面积为:"+s);

// textview.settext("该长方体的表面积为:"+a);
 }


}

3.activity_biapmianji.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.flyyu.four.biapmianji">

 <textview
 android:id="@+id/textview4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginbottom="8dp"
 android:layout_marginleft="8dp"
 android:layout_marginright="8dp"
 android:layout_margintop="8dp"
 android:text="textview"
 android:textsize="18sp"
 app:layout_constraintbottom_tobottomof="parent"
 app:layout_constrainthorizontal_bias="0.174"
 app:layout_constraintleft_toleftof="parent"
 app:layout_constraintright_torightof="parent"
 app:layout_constrainttop_totopof="parent"
 app:layout_constraintvertical_bias="0.083" />
</android.support.constraint.constraintlayout>

4.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<!--<android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"-->

<android.widget.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="com.example.flyyu.four.mainactivity">

 <textview
 android:id="@+id/textview"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="长:" />

 <edittext
 android:id="@+id/edittext"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:ems="10"
 android:inputtype="text" />

 <textview
 android:id="@+id/textview2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="宽:" />

 <edittext
 android:id="@+id/edittext2"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:ems="10"
 android:inputtype="text" />

 <textview
 android:id="@+id/textview3"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="高:" />

 <edittext
 android:id="@+id/edittext3"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:ems="10"
 android:inputtype="text" />

 <button
 android:id="@+id/button"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:onclick="onclick"
 android:text="计算" />
</android.widget.linearlayout>

更多计算器功能实现,请点击专题: 计算器功能汇总 进行学习

关于android计算器功能的实现,查看专题:android计算器 进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网