exam01--2
终于让我完整地做了一道题,哭泣器
效果图
\res\values\string.xml
<resources>
<string name="app_name">exam2</string>
<string-array name="data">
<item>+</item>
<item>-</item>
<item>*</item>
<item>÷</item>
</string-array>
</resources>
axtivity.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=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/id_edittext1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
<TextView
android:id="@+id/id_textview_symbol"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"/>
<Spinner
android:id="@+id/id_spinner"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</Spinner>
<EditText
android:id="@+id/id_edittext2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
<Button
android:id="@+id/id_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="=" />
<TextView
android:id="@+id/id_textview_result"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"/>
</LinearLayout>
<ListView
android:id="@+id/id_listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
MainActivity.java
package com.example.exam2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText editText1,editText2;
private TextView textView_symbol,textView_result;
private Spinner spinner;
private Button btn_equal;
private ListView listView;
private List<String> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//获取xml文件中字符串数组
final String datas[] = getResources().getStringArray(R.array.data);
//初始化适配器
ArrayAdapter adapter1 = new ArrayAdapter(this,android.R.layout.simple_list_item_1,datas);
final ArrayAdapter adapter2 = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
//绑定适配器
spinner.setAdapter(adapter1);
listView.setAdapter(adapter2);
//spinner的点击事件
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textView_symbol.setText(datas[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//Button的点击事件
btn_equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView_result.setText(cal(textView_symbol.getText()+"",editText1.getText()+"",editText2.getText()+""));
String str = editText1.getText()+""+textView_symbol.getText()+editText2.getText()+"="+textView_result.getText();
list.add(str);
adapter2.notifyDataSetChanged();
}
});
}
public String cal(String symbol,String first,String second){
double s=0;
Double d1 = Double.parseDouble(first);
Double d2 = Double.parseDouble(second);
switch (symbol){
case "+":
s=d1+d2;
break;
case "-":
s=d1-d2;
break;
case "*":
s=d1*d2;
break;
case "÷":
s=d1/d2;
break;
}
return s+"";
}
public void initView(){
editText1 = (EditText)findViewById(R.id.id_edittext1);
editText2 = (EditText)findViewById(R.id.id_edittext2);
textView_symbol = (TextView)findViewById(R.id.id_textview_symbol);
textView_result = (TextView)findViewById(R.id.id_textview_result);
spinner = (Spinner)findViewById(R.id.id_spinner);
btn_equal = (Button)findViewById(R.id.id_btn);
listView = (ListView)findViewById(R.id.id_listview);
}
}
努力做最期待的自己。