I
I
igorpowers2020-11-25 19:16:12
Java
igorpowers, 2020-11-25 19:16:12

How to multiply a number from editTextr with another number?

Hey guys, sorry that I want so much, I'm just learning Java programming for now. It seems to have taken a course in SoloLearn, but still somehow muddy. In general, now I'm trying to make it so that a person enters the number of grams in the editText field, selects a product from the Spinner, and the application counts the number of calories. Well, respectively, in the code I indicate figuratively if (apple selected) {display (editText * 0.52)}. You do not think that I'm straight zero. I made a condition, a method, and so on, but when I clicked on the button, FirstApp has stopped, it crashed and that's it ..
here's the code

package com.example.firstapp;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;


public class MainActivity extends AppCompatActivity {

    EditText inputText;
    Button encBtn;

    public final String[] productsList = {"Яблоко","Картофель","Говядина","Апельсин"};

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


        ArrayAdapter<String> productsAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, productsList);
        productsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner productsList = findViewById(R.id.spProducts);
        productsList.setPrompt("Выберите продукт");

        productsList.setAdapter(productsAdapter);

        encBtn = findViewById(R.id.encBtn);
        inputText = findViewById(R.id.inputText);

    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (v instanceof EditText) {
                EditText edit = ((EditText) v);
                Rect outR = new Rect();
                edit.getGlobalVisibleRect(outR);
                boolean isKeyboardOpen = !outR.contains((int)ev.getRawX(), (int)ev.getRawY());
                System.out.print("Is Keyboard? " + isKeyboardOpen);
                if (isKeyboardOpen) {
                    System.out.print("Entro al IF");
                    edit.clearFocus();
                    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
                }

                edit.setCursorVisible(!isKeyboardOpen);


            }
        }
        return super.dispatchTouchEvent(ev);
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2020-11-25
@igorpowers

Good afternoon!
In order to objectively answer your question, you have provided neither a code snippet nor a stacktrace. And this complicates the task, because it is not clear what kind of problem you are facing and how to overcome it.

It seems to have taken a course in SoloLearn

Believe the person who played with this program and "passed" this course for fun, this is absolutely complete nonsense. You won't learn anything from this course. If you really want to learn Java & Android from tutorials, then look towards startandroid udemy javarush, paid courses, etc. But I will say in advance that at some point you will lack this knowledge anyway and you will have to download books and read them.
G. Schildt - The Complete Java Guide (10th Edition)
Java Head First et al.
As my colleagues have answered - you may be running into a type casting problem. And it is possible that you don’t get to that and another exception is thrown. In order not to be unfounded, provide your source code, as well as a stack trace (error log).

D
Developer, 2020-11-25
@samodum

You need to deal with types. You're multiplying a String by a number, but you can't do that.
Read a book on the basics of a programming language. In your case, it's Java.
Here's another piece of evidence against fucking courses. Elementary things cannot be taught.
Where is the code? Where are the logs? Didn't they teach this in the course?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question