(AnroidStudio)⽤简单代码实现BMI计算器并且将应⽤安装
到⼿机上
第⼀步:创建⼯程:
第⼆步:编写前端页⾯:
找到l:
具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
&straintlayout.widget.ConstraintLayout xmlns:android="/apk/res/android" xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:id="@+id/nav_host_fragment_content_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="invisible"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="@navigation/nav_graph" />
<TextView
android:id="@+id/textView"
android:layout_width="168dp"
android:layout_height="93dp"
android:text="@string/label"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.269" />
<TextView
android:id="@+id/textView2"
android:layout_width="63dp"
android:layout_height="26dp"
android:layout_marginStart="52dp"
android:layout_marginTop="32dp"
android:text="⾝⾼(cm)"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<!--app代表上下左右位置-->
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="@string/tips"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.255"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView3"
android:layout_width="60dp"
android:layout_height="23dp"
android:layout_marginStart="52dp"
android:layout_marginTop="48dp"
android:text="体重(kg)"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="@string/tips2"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/textView3"阿勇河漂流
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="184dp"
android:onClick="onClick"
语言特色有哪些android:text="@string/count"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.192" />
<TextView
android:id="@+id/textView4"
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_marginTop="12dp"
app:layout_constraintEnd_toEndOf="parent"画皮网团购
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName2" /> </straintlayout.widget.ConstraintLayout>
第三步:编写后端:
找到MainActivity类:⾥⾯需要注意编写的每⼀个组件的id需要⼀致。application777;
import Edits;
import android.os.Bundle;
le.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
application777.databinding.ActivityMainBinding;
水浒传人物形象分析
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
冈的组词private AppBarConfiguration appBarConfiguration;
private ActivityMainBinding binding;
/
/计算
public float height=0f;
public float weight=0f;
public float result=0f;
//输⼊框的属性
EditText editText1=null;
EditText editText2=null;
TextView textView =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
binding = ActivityMainBinding.inflate(getLayoutInflater());
Root());
lbar);
//实例化
//⾝⾼体重
editText1=(EditText)findViewById(R.id.editTextTextPersonName);
editText2=(EditText)findViewById(R.id.editTextTextPersonName2);
//显⽰结果
//显⽰结果
textView=(TextView)findViewById(View4);
神舟五号载人飞船
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_m
ain); appBarConfiguration = new AppBarConfiguration.Graph()).build();
NavigationUI.tupActionBarWithNavController(this, navController, appBarConfiguration);
binding.fab.tOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.tAction("Action", null).show();
}
});
}
//计算
public void onClick(View view){
Log.d("TAG","--------点击了计算按钮-------");
height= Float.Text().toString());
height=height/100f;
weight= Float.Text().toString());
result=weight/(height*height);
textView.tText("您的BMI值为:"+result);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is prent.
getMenuInflater().u.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity l.
int id = ItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_ttings) {
补刀return true;
}
OptionsItemSelected(item);
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main); return NavigationUI.navigateUp(navController, appBarConfiguration)
|| SupportNavigateUp();
}
}
第四步:调试
书包里的故事