您好,登錄后才能下訂單哦!
這篇文章主要講解了Android Studio如何實(shí)現(xiàn)長(zhǎng)方體表面積計(jì)算器,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
寫(xiě)了兩個(gè)方法(在我理解之中有三個(gè)方法,其中循環(huán)字符串匹配太low了,pass掉),目前先上傳一個(gè),后續(xù)補(bǔ)上。
針對(duì)這個(gè)問(wèn)題 總共有四個(gè)文件
方法一:正則表達(dá)式
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("該長(zhǎng)方體的表面積為:"+s); // textView.setText("該長(zhǎng)方體的表面積為:"+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="長(zhǎng):" /> <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="計(jì)算" /> </android.widget.LinearLayout>
看完上述內(nèi)容,是不是對(duì)Android Studio如何實(shí)現(xiàn)長(zhǎng)方體表面積計(jì)算器有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。