xml 使用微调器并进行数学运算

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml 使用微调器并进行数学运算相关的知识,希望对你有一定的参考价值。

public class MainActivity extends AppCompatActivity {

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

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.operators, R.layout.support_simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

    }

    public void math(View view) {
        EditText is = (EditText) findViewById(R.id.Int1);
        double ib = Double.parseDouble(is.getText().toString());
        EditText ia = (EditText) findViewById(R.id.Int2);
        double ix = Double.parseDouble(ia.getText().toString());

        TextView t = (TextView) findViewById(R.id.result);

        double result;

        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        String operator = spinner.getSelectedItem().toString();

        switch (operator){
            case "+":
                result = ib + ix;
                break;
            case "-":
                result = ib - ix;
                break;
            case "*":
                result = ib * ix;
                break;
            case "/":
                result = ix / ib;
                break;
            default:
                result = 0;
                break;
        }
        t.setText(result + "");
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.tribbyx.mysimplecalculator.MainActivity"
    android:orientation="vertical">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:layout_below="@+id/Int1"
        android:layout_alignEnd="@+id/Int1"
        android:layout_marginTop="52dp"
        android:id="@+id/Int2" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/Int1"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner" />

    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="11dp"
        android:id="@+id/button"
        android:layout_below="@+id/Int2"
        android:layout_centerHorizontal="true"
        android:onClick="math"/>

    <TextView
        android:text=""
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_alignParentStart="true"
        android:layout_marginStart="146dp"
        android:layout_marginTop="57dp"
        android:id="@+id/result" />
</LinearLayout>
<resources>
    <string name="app_name">MySimpleCalculator</string>
    <string-array name="operators">
        <item>+</item>
        <item>-</item>
        <item>*</item>
        <item>/</item>
    </string-array>
</resources>

以上是关于xml 使用微调器并进行数学运算的主要内容,如果未能解决你的问题,请参考以下文章

从 sql 填充微调器并使结果独一无二

如何从微调器中检索数据并进行处理?

Android微调器使用XML进行数据绑定并显示所选值

在显示加载微调器之前等待?

如何将多个用户输入保存到微调器?

Android微调器没有显示Firebase实时数据库数据?