AI智能
改变未来

android利用spinner选择加减乘除进行运算!!!!

#android利用spinner选择加减乘除进行运算!!!!
##效果图

##代码部分
.xml

<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"android:layout_width=\"fill_parent\"android:layout_height=\"fill_parent\" ><EditTextandroid:id=\"@+id/edt_test1\"android:layout_width=\"60dip\"android:layout_height=\"40dip\"android:ems=\"10\"android:inputType=\"number\"android:textColor=\"#88e355\"android:textSize=\"20sp\" /><Spinnerandroid:id=\"@+id/spinner\"android:layout_width=\"60dip\"android:layout_height=\"40dip\"/><EditTextandroid:id=\"@+id/edt_test2\"android:layout_width=\"60dip\"android:layout_height=\"40dip\"android:ems=\"10\"android:inputType=\"number\"android:textColor=\"#88e355\"android:textSize=\"20sp\" /><Buttonandroid:id=\"@+id/button1\"android:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"=\" /><TextViewandroid:id=\"@+id/tv\"android:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"\" /></LinearLayout>

.java

package com.example.test1_2;import android.app.Activity;import android.app.AlertDialog;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.Spinner;import android.widget.TextView;public class MainActivity extends Activity {private Spinner spinner;private EditText editNum1;private EditText editNum2;private TextView result;private Button button;private String[] huansaun = {\"+\",\"-\",\"×\",\"÷\"};String flag;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);spinner = (Spinner) findViewById(R.id.spinner);editNum1 = (EditText) findViewById(R.id.edt_test1);editNum2 = (EditText) findViewById(R.id.edt_test2);button = (Button) findViewById(R.id.button1);result = (TextView) findViewById(R.id.tv);ArrayAdapter<String> huansuan_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,huansaun);spinner.setAdapter(huansuan_adapter);spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {flag = spinner.getSelectedItem().toString();Log.e(\"Dawangba\", String.valueOf(position));}@Overridepublic void onNothingSelected(AdapterView<?> parent) {}});button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (flag.equals(\"+\")){AddListener addListener = new AddListener();addListener.onClick(v);}if (flag.equals(\"-\")){SubListener subListener = new SubListener();subListener.onClick(v);}if (flag.equals(\"×\")){MulListener mulListener = new MulListener();mulListener.onClick(v);}if (flag.equals(\"÷\")){DivListener divListener = new DivListener();divListener.onClick(v);}}});}private void showDialog(){AlertDialog.Builder dialog = new AlertDialog.Builder(this);dialog.setTitle(\"错误\");dialog.setMessage(\"输入数字无效!\");dialog.setPositiveButton(\"确定\",null);dialog.show();}//加法操作private class AddListener implements View.OnClickListener {public void onClick(View v) {if (TextUtils.isEmpty(editNum1.getText().toString()) || TextUtils.isEmpty(editNum2.getText().toString())){showDialog();} else {float number1 = Float.parseFloat((editNum1.getText().toString()));float number2 = Float.parseFloat((editNum2.getText().toString()));result.setText(number1 + number2 + \"\");}}}//减法操作private class SubListener implements View.OnClickListener {public void onClick(View v) {if (TextUtils.isEmpty(editNum1.getText().toString()) || TextUtils.isEmpty(editNum2.getText().toString())){showDialog();} else {float number1 = Float.parseFloat((editNum1.getText().toString()));float number2 = Float.parseFloat((editNum2.getText().toString()));result.setText(number1 - number2 + \"\");}}}//乘法操作private class MulListener implements View.OnClickListener {public void onClick(View v) {if (TextUtils.isEmpty(editNum1.getText().toString()) || TextUtils.isEmpty(editNum2.getText().toString())){showDialog();} else {float number1 = Float.parseFloat((editNum1.getText().toString()));float number2 = Float.parseFloat((editNum2.getText().toString()));result.setText(number1 * number2 + \"\");}}}//除法操作private class DivListener implements View.OnClickListener {public void onClick(View v) {if (TextUtils.isEmpty(editNum1.getText().toString()) || TextUtils.isEmpty(editNum2.getText().toString())){showDialog();} else {float number1 = Float.parseFloat((editNum1.getText().toString()));float number2 = Float.parseFloat((editNum2.getText().toString()));if (number2 == 0){showDialog();} else {result.setText(number1 / number2 + \"\");}}}}}

部分参照网络,些许bug,可以简单实现!!!

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » android利用spinner选择加减乘除进行运算!!!!