package com.example.myapplication; import android.annotation.SuppressLint; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.google.android.material.bottomnavigation.BottomNavigationView; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.navigation.NavController; import androidx.navigation.Navigation; import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView result; private Button btn1; private Button btn2; private Button btn3; private Button btn4; private EditText edit1; private EditText edit2; public MainActivity() { } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BottomNavigationView navView = findViewById(R.id.nav_view); // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications) .build(); NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); NavigationUI.setupWithNavController(navView, navController); btn1 = (Button) findViewById(R.id.btn111); btn2 = (Button) findViewById(R.id.btn222); btn3 = (Button) findViewById(R.id.btn333); btn4 = (Button) findViewById(R.id.btn444); result=(TextView) findViewById(R.id.result); edit1=(EditText) findViewById(R.id.edi1); edit2=(EditText) findViewById(R.id.edi2); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); } // public void aaa(View view){ // Toast.makeText(MainActivity.this, "按钮被点击",0).show(); // } @Override public void onClick(View v) { final AlertDialog.Builder alterDiaglog = new AlertDialog.Builder(MainActivity.this); alterDiaglog.setTitle("简单的dialog");//文字 alterDiaglog.setMessage("是否计算");//提示消息 //积极的选择 alterDiaglog.setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"计算成功",Toast.LENGTH_SHORT).show(); } }); //消极的选择 alterDiaglog.setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"结束",Toast.LENGTH_SHORT).show(); MainActivity.this.finish(); } }); //中立的选择 alterDiaglog.setNeutralButton("不进行计算", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"不计算",Toast.LENGTH_SHORT).show(); } }); //显示 alterDiaglog.show(); int num1 =Integer.parseInt(edit1.getText().toString()); int num2 =Integer.parseInt(edit2.getText().toString()); int result1 =0; System.out.println(num1); System.out.println(num2); switch (v.getId()) { case R.id.btn111: result1=num1 + num2; System.out.println(result); break; case R.id.btn222: result1=num1 - num2; break; case R.id.btn333: result1=num1 * num2; break; case R.id.btn444: result1=num1 / num2; break; } result.setText(result1+""); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="?attr/actionBarSize"> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/nav_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" android:background="?android:attr/windowBackground" android:layout_alignParentBottom="true"/> <TextView android:id="@+id/text_notifications" android:layout_width="490dp" android:layout_height="46dp" android:layout_alignParentBottom="true" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:textAlignment="center" android:textSize="20sp" /> <EditText android:id="@+id/edi1" android:layout_width="600dp" android:layout_height="80dp" /> <EditText android:id="@+id/edi2" android:layout_width="600dp" android:layout_height="80dp" android:layout_below="@+id/edi1" /> <Button android:id="@+id/btn111" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edi2" android:layout_marginLeft="20dp" android:layout_marginTop="50dp" android:text="+" /> <Button android:id="@+id/btn222" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edi2" android:layout_marginTop="50dp" android:text="-" android:layout_toRightOf="@+id/btn111" /> <Button android:id="@+id/btn333" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edi2" android:text="*" android:layout_marginTop="50dp" android:layout_toRightOf="@+id/btn222" /> <Button android:id="@+id/btn444" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edi2" android:layout_marginTop="50dp" android:text="/" android:layout_toRightOf="@+id/btn333" /> <TextView android:id="@+id/te" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="计算结果" android:textSize="40dp" android:layout_marginTop="50dp" android:layout_below="@+id/btn444" /> <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40dp" android:layout_below="@+id/te" android:layout_marginTop="50dp" /> <fragment android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:defaultNavHost="true" app:layout_constraintBottom_toTopOf="@id/nav_view" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/mobile_navigation" /> </RelativeLayout>