简体   繁体   中英

Why I can't run the two activities together? the two activities and the image shows that I reached MainActivity directly without clicking homebutton

knowing that there is no errors, only MainActivity shows without homebutton................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

MainActivity.java

package com.example.AppCalculator;
    
    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 {
        EditText etfirstvalue,etsecondvalue;
        Button btnadd,btnsubs,btnmultiply,btndivide;
    
        Double num1,num2;
        TextView tvresult;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            etfirstvalue=findViewById(R.id.etfirstvalue);
            etsecondvalue=findViewById(R.id.etsecondvalue);
            btnadd=findViewById(R.id.btnadd);
            btndivide=findViewById(R.id.btndivision);
            btnmultiply=findViewById(R.id.btnmultiply);
            btnsubs=findViewById(R.id.btnsubs);
            tvresult=findViewById(R.id.tvresult);
    
            Clicklistener();
        }
        public void Clicklistener(){
            btnadd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    num1=Double.parseDouble(etfirstvalue.getText().toString());
                    num2=Double.parseDouble(etsecondvalue.getText().toString());
                    Double result=num1+num2;
                    tvresult.setText(String.valueOf(result));
                }
            });
            btnsubs.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
    
                    num1=Double.parseDouble(etfirstvalue.getText().toString());
                    num2=Double.parseDouble(etsecondvalue.getText().toString());
                    Double result=num1-num2;
                    tvresult.setText(String.valueOf(result));
                }
            });
            btnmultiply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    num1=Double.parseDouble(etfirstvalue.getText().toString());
                    num2=Double.parseDouble(etsecondvalue.getText().toString());
                    Double result=num1*num2;
                    tvresult.setText(String.valueOf(result));
                }
            });
            btndivide.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    num1=Double.parseDouble(etfirstvalue.getText().toString());
                    num2=Double.parseDouble(etsecondvalue.getText().toString());
                    Double result=num1/num2;
                    tvresult.setText(String.valueOf(result));
                }
            });
        }
    }

MainActivity.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:background="#FFFFFF"
    android:backgroundTint="#FFFFFF"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:text="Simple Calculator"
        android:textSize="25sp" />

    <EditText
        android:id="@+id/etfirstvalue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="Enter First Value"
        android:inputType="number" />

    <EditText
        android:id="@+id/etsecondvalue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="Enter Second Value"
        android:inputType="number" />

    <TextView
        android:id="@+id/tvresult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:text="Result"
        android:textColor="@color/purple_500"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_marginHorizontal="10dp"
        android:orientation="horizontal"
        android:weightSum="2">

        <Button
            android:id="@+id/btnadd"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="10dp"
            android:layout_weight="1"
            android:backgroundTint="@color/purple_500"
            android:text="ADD" />


        <Button
            android:id="@+id/btnsubs"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="10dp"
            android:layout_weight="1"
            android:backgroundTint="@color/purple_500"
            android:text="Subs" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_marginHorizontal="10dp"
        android:orientation="horizontal"
        android:weightSum="2">

        <Button
            android:id="@+id/btnmultiply"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="10dp"
            android:layout_weight="1"
            android:backgroundTint="@color/purple_500"
            android:text="Multiply" />


        <Button
            android:id="@+id/btndivision"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="10dp"
            android:layout_weight="1"
            android:backgroundTint="@color/purple_500"
            android:text="Divide" />


    </LinearLayout>
</LinearLayout>

homebutton.java

package com.example.AppCalculator;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class homebutton extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homebutton);
        Button btncalc=findViewById(R.id.btncalc);
        btncalc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(homebutton.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

homebutton.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"
    tools:context=".homebutton"
    tools:ignore="ExtraText">

    <Button
        android:id="@+id/btncalc"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="10dp"
        android:layout_weight="1"
        android:backgroundTint="@color/purple_500"
        android:text="Calculator" />
</LinearLayout>

You need to clarify your question more. However I believe you meant homebutton should start first and after you click calculate it should take you to the second one.

If that's the case, then you need to define your start activity from the AndroidManifest because currently your app is starting on MainActivity.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM