PS:对于直接在码云上clone项目的同学:由于两个实验是一起写的,因此在AndroidManifest即安卓的注册文件中,要将FlashActivity底下的
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
这行内容剪切粘贴到MainActivity下。即:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Flashlight">
</activity>
最近Android老师讲了个Spinner控件,由于以前没有接触过于是写下此笔记,哪天要用了再回来翻翻。
1.首先还是我们喜闻乐见的XML布局文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="数字1:" />
<EditText
android:id="@+id/numEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请在此输入" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="运算符"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20sp">
</Spinner>
<TextView
android:text="数字2"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/numEdit2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请在此输入" />
<Button
android:id="@+id/jisuan"
android:text="计算"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="结果:"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
基本上实现了和作业要求一样的布局文件。我觉得布局文件这地方都能弄懂不用我再来赘述了吧。。。
2.然后就是JAVA代码实现的部分了:
先贴上全部代码吧,然后再试着讲解一下各个部分功能:
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
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 AppCompatActivity {
private Spinner spinner;
private EditText editNum1;
private EditText editNum2;
private TextView result;
private Button button;
private String[] huansaun = {"+","-","*","/"};
String flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
editNum1 = (EditText) findViewById(R.id.numEdit);
editNum2 = (EditText) findViewById(R.id.numEdit2);
button = (Button) findViewById(R.id.jisuan);
result = (TextView) findViewById(R.id.result);
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() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
flag = spinner.getSelectedItem().toString();
Log.e("Dawangba", String.valueOf(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public 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 + "");
}
}
}
}
}
主要说明一下JAVA代码吧:
1.首先,先将加减乘除四个运算符写入huansuan数组中,这里直接
private String[] huansaun = {"+","-","*","/"};
之后,写一个huansuan_adapter,用于将加减乘除显示在点击了spinner后弹出的框内,并且为spinner控件设置huansuan_adapter:
ArrayAdapter<String> huansuan_adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_dropdown_item,huansaun
);
spinner.setAdapter(huansuan_adapter);
之后设置spinner的选择事件:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
flag = spinner.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
在这里我设置了一个flag,以此获取spinner控件选择的值,请注意这个flag,这将很有用。
我们再写加减乘除这四个监听事件:
//加法操作
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 + "");
}
}
}
}
由于要能够实现小数的加减乘除,因此使用了float来定义number1与number2;
并且为了能够达到输入为空时弹出提示框的方法,还写了一个showDialog()方法,代码如下:
private void showDialog(){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("错误");
dialog.setMessage("输入数字无效!");
dialog.setPositiveButton("确定",null);
dialog.show();
}
之后,便是万年不变的button点击的监听事件辣,代码如下,我们可以在这里看到flag的作用:
button.setOnClickListener(new View.OnClickListener() {
@Override
public 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);
}
}
});
(注)button的点击事件这里是写在onCreate方法下的。
附上本次源码链接(内含实验第二部分开关灯的源码):https://gitee.com/bbchond/Android-Spinner-Test