TextView中的整数值不会更新; onClickListener不会运行代码

时间:2015-02-11 02:10:07

标签: java android xml integer onclicklistener

无论我为这个分配编辑或更改我的Java代码有多少,它都拒绝正确更新我为结果设置的整数变量的值。有人能告诉我我在这里失踪的是什么吗?以下是我的代码:     package com.example.minicalc;

import com.example.minicalc.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class MainActivity extends Activity implements OnEditorActionListener {
    private static final String TAG = "MiniCalc";
    private SharedPreferences savedValues;

    private EditText editText1;
    private EditText editText2;
    private Button button1;
    private Spinner spinner1;
    private TextView answerLbl;
    private int ivalue1 = 0;
    private int ivalue2 = 0;
    private int ianswer;
    private int spinChoice;
    //private String[] spinner_strings = {"+", "-", "×", "÷"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate event started");
        setContentView(R.layout.activity_main);

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        button1 = (Button) findViewById(R.id.button1);
        answerLbl = (TextView) findViewById(R.id.answerTxt);
        editText1 = (EditText) findViewById(R.id.editText1);
        editText2 = (EditText) findViewById(R.id.editText2);

        editText1.setOnEditorActionListener(this);
        editText2.setOnEditorActionListener(this);

        savedValues = getSharedPreferences("SavedValues", MODE_PRIVATE);

        spinChoice = spinner1.getSelectedItemPosition();

        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Log.d(TAG, "onClick event started");
                ianswer = 0;
                if (spinChoice == 0) {
                    ianswer = ivalue1 + ivalue2;
                    Log.d(TAG, "ianswer VALUE IS: " + Integer.valueOf(ianswer).toString());
                } else if (spinChoice == 1) {
                    ianswer = ivalue1 - ivalue2;
                    Log.d(TAG, "ianswer VALUE IS: " + Integer.valueOf(ianswer).toString());
                } else if (spinChoice == 2) {
                    ianswer = ivalue1 * ivalue2;
                    Log.d(TAG, "ianswer VALUE IS: " + Integer.valueOf(ianswer).toString());
                } else if (spinChoice == 3) {
                    ianswer = ivalue1 / ivalue2;
                    Log.d(TAG, "ianswer VALUE IS: " + Integer.valueOf(ianswer).toString());
                }
                answerLbl.setText(Integer.toString(ianswer));
                //answerLbl.setText(""+ianswer);
                Log.d(TAG, "answerLbl TEXT IS: " + answerLbl.getText());
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        Log.d(TAG, "onEditorAction event started");
        if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
            ivalue1 = Integer.parseInt(editText1.getText().toString());
            ivalue2 = Integer.parseInt(editText2.getText().toString());
        }
        return false;
    }

    @Override
    public void onPause() {
        Log.d(TAG, "onPause event started");
        Editor editor = savedValues.edit();
        editor.putString("value1", editText1.getText().toString());
        editor.putString("value2", editText2.getText().toString());

        editor.commit();
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume event started");
        editText1.setText(savedValues.getString("value1", ""));
        editText2.setText(savedValues.getString("value2", ""));
    }
}

这是我的布局XML,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"
    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.minicalc.MainActivity" >
    <TextView
        android:id="@+id/valLbl1"
        android:labelFor="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="40dp"
        android:text="@string/valueLbl1" />
    <TextView
        android:id="@+id/valLbl2"
        android:labelFor="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/valLbl1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="24dp"
        android:text="@string/valueLbl2" />
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/valLbl1"
        android:layout_below="@+id/valLbl1"
        android:layout_marginTop="20dp"
        android:ems="5"
        android:inputType="number">
        <requestFocus />
    </EditText>
    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/valLbl2"
        android:layout_below="@+id/valLbl2"
        android:layout_marginTop="16dp"
        android:ems="5"
        android:inputType="number" />
    <TextView
        android:id="@+id/answerTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/answerLblTxt"
        android:layout_centerHorizontal="true"
        android:ems="7"/>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="@string/submit" />
    <TextView
        android:id="@+id/answerLblTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/editText2"
        android:layout_below="@+id/button1"
        android:layout_marginTop="16dp"
        android:text="@string/answerLbl" />
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/valLbl2"
        android:layout_marginStart="28dp"
        android:layout_toEndOf="@+id/editText2"
        android:entries="@array/spinner_strings" />
</RelativeLayout>

现在,应用程序运行正常,LogCat消息显示正常...除非我将它们放在onClickListener中。所以,是的,我已经确定了问题所在,我只是不知道问题是什么。我会显示错误的屏幕截图,但这是我第一次发布。在任何情况下,运行应用程序的手机都是三星Galaxy Note 3,运行Android 4.4.4。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您的微调器getSelectedItemPoisition()方法仅在onCreate()方法中调用一次,并且每次单击按钮时都不会调用它,导致值永远不会更新。

button1.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {

        spinChoice = spinner1.getSelectedItemPosition();

        ...
    }

这行代码需要在onClick()方法中添加,导致在按钮点击时读取新值。