ResultatPrise.java :  » Game » compte-les-points » com » moanoit » belote » Android Open Source

Android Open Source » Game » compte les points 
compte les points » com » moanoit » belote » ResultatPrise.java
package com.moanoit.belote;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.moanoit.belote.bl.CalculateScore;

public class ResultatPrise extends Activity {

  private EditText mResult1;
  private EditText mResult2;
  private CheckBox mCapot;
  private CheckBox mBelotte;


  /**
   * Called when the activity is first created.
   * @param savedInstanceState saved state
   */
  /* (non-Javadoc)
   * @see android.app.Activity#onCreate(android.os.Bundle)
   */
  @Override
  protected final void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resultat_prise);
    setTitle(R.string.prise_edit_title);

    // Entres utilisateur
    mResult1 = (EditText) findViewById(R.id.resultat_prise_team1);
    mResult2 = (EditText) findViewById(R.id.resultat_prise_team2);
    mCapot = (CheckBox) findViewById(R.id.resultat_prise_capot);
    mBelotte = (CheckBox) findViewById(R.id.resultat_prise_belotte);

    Button confirmButton = (Button) findViewById(R.id.resultat_prise_ok);
    Button cancelButton = (Button) findViewById(R.id.resultat_prise_cancel);

    // gestion du bouton OK
    confirmButton.setOnClickListener(new View.OnClickListener() {

      public void onClick(View view) {
        Bundle bundle = new Bundle();

        String result1Txt = mResult1.getText().toString();
        String result2Txt = mResult2.getText().toString();
        boolean capot = mCapot.isChecked();
        int result = 0;

        if (result1Txt.equals("") && result2Txt.equals("") && !capot) {
          Toast.makeText(ResultatPrise.this,
              R.string.result_prise_no_result,
              Toast.LENGTH_SHORT).show();
          return;
        } else {
          if (capot) {
            result = Constantes.POINTS_CAPOT;
          } else {
            if (result2Txt.equals("")) {
              result = Integer.parseInt(result1Txt);
            } else {
              if (result1Txt.equals("")) {
                result = Constantes.TOTAL_POINTS
                  - Integer.parseInt(result2Txt);
              } else {
                int result1 = Integer.parseInt(result1Txt);
                int result2 = Integer.parseInt(result2Txt);
                if (!CalculateScore.totalCorrect(result1,
                    result2)) {
                  Toast.makeText(ResultatPrise.this,
                      R.string.result_prise_incorrect,
                      Toast.LENGTH_SHORT).show();
                  return;
                }
              }
            }
          }
        }

        boolean belotte = mBelotte.isChecked();

        Intent mIntent = new Intent();
        bundle.putInt(Constantes.PRISE_RESULT_NUMBER, result);
        bundle.putBoolean(Constantes.BELOTE, belotte);
        bundle.putBoolean(Constantes.CAPOT, capot);
        mIntent.putExtras(bundle);
        setResult(Constantes.PRISE_RESULT, mIntent);
        finish();
      }

    });

    cancelButton.setOnClickListener(new View.OnClickListener() {

      public void onClick(View view) {
        finish();
      }

    });

    OnKeyListener keyListener = new OnKeyListener() {
      @Override
      public boolean onKey(View v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_DOWN)
            && (keyCode == KeyEvent.KEYCODE_ENTER)) {
          // Perform action on key press
          ((EditText) v)
          .setText(CalculateScore.roundNumericEditTextValue(v));
          return true;
        }
        return false;
      }
    };

    OnFocusChangeListener focusListener = new OnFocusChangeListener() {
      @Override
      public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
          ((EditText) v).setText(
              CalculateScore.roundNumericEditTextValue(v));
        }
      }
    };

    mResult1.setOnKeyListener(keyListener);
    mResult2.setOnKeyListener(keyListener);
    mResult1.setOnFocusChangeListener(focusListener);
    mResult2.setOnFocusChangeListener(focusListener);
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.