微调器的条件语句

时间:2018-05-21 05:10:40

标签: java android conditional android-spinner

我在android中了解spinner有一些问题

的strings.xml

<string-array name="pickformula">
    <item>add</item>
    <item>multiply</item>
</string-array>

xml文件

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner1"
    android:entries="@array/pickformula"
    android:layout_marginLeft="10dp"/>
<Spinner

 <Button
     android:id="@+id/button1"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="calc" />
 <EditText
     android:id="@+id/input1"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:inputType="number"/>
<EditText
     android:id="@+id/input2"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:inputType="number"/>

爪哇

package com.aururatech.dropvoltage3;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );
  }
}

这就像简单的计算器。 如果我选择添加微调器,按下按钮时的公式是= input1+input2

如果我在微调器中选择乘法,我按下按钮时的公式是= input1*input2

我需要该示例的java代码.. 谢谢

1 个答案:

答案 0 :(得分:1)

请按照这些代码解决您的问题。在这个我根据你创建一个计算器。所有操作都通过微调更改来执行。

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
  Spinner operation;
  Button calculate;
  EditText input1;
  EditText input2;
  TextView result;
  String val;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    operation = findViewById(R.id.spinner1);
    calculate = findViewById(R.id.button1);
    input1 = findViewById(R.id.input1);
    input2 = findViewById(R.id.input2);
    result = findViewById(R.id.textView);

    calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String val1 = input1.getText().toString();
            String val2 = input2.getText().toString();
            if (val.equals("add")) {
                result.setText(String.valueOf(Integer.parseInt(val1) + Integer.parseInt(val2)));
            }
            if (val.equals("sub")) {
                result.setText(String.valueOf(Integer.parseInt(val1) - Integer.parseInt(val2)));
            }
            if (val.equals("multiply")) {
                result.setText(String.valueOf(Integer.parseInt(val1) * Integer.parseInt(val2)));
            }
            if (val.equals("divide")) {
                result.setText(String.valueOf(Integer.parseInt(val1) / Integer.parseInt(val2)));
            }
        }
    });

    operation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            val = operation.getSelectedItem().toString();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
  }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner1"
        android:entries="@array/pickformula" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="calc" />

    <EditText
        android:id="@+id/input1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:inputType="number" />

    <EditText
        android:id="@+id/input2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/input1"
        android:layout_marginTop="28dp"
        android:inputType="number" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp" />

</RelativeLayout>

string.xml

<resources>
  <string-array name="pickformula">
    <item>add</item>
    <item>sub</item>
    <item>multiply</item>
    <item>divide</item>
  </string-array>

</resources>