1 Replies - 521 Views - Last Post: 12 November 2015 - 02:23 PM

#1 proprsk8   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 41
  • Joined: 10-November 13

Why can't user edit the editText?

Posted 01 November 2015 - 10:14 AM

Hi, I'm very new to developing apps and need help figuring out why my Party Size field will not allow the user to change the one to a different number. The problem I'm running into is that I set the partySize intiially to one but acts like a textView and only displays the one and the user can't edit it at all. I believe I did everything that I'm supposed but it just seems that I'm missing something.

Below is my code the field I'm referencing to is with the prefix party:
package com.deitel.tipcalculator;

import java.text.NumberFormat; // for currency formatting

import android.app.Activity; // base class for activities
import android.os.Bundle; // for saving state information
import android.text.Editable; // for EditText event handling
import android.text.TextWatcher; // EditText listener
import android.widget.EditText; // for bill amount input
import android.widget.SeekBar; // for changing custom tip percentage
import android.widget.SeekBar.onseekBarChangeListener; // SeekBar listener
import android.widget.TextView; // for displaying text

// MainActivity class for the Tip Calculator app
public class MainActivity extends Activity 
{
   // currency and percent formatters 
   private static final NumberFormat currencyFormat = 
      NumberFormat.getCurrencyInstance();
   private static final NumberFormat percentFormat = 
      NumberFormat.getPercentInstance();
   

   private static final NumberFormat partyFormat = NumberFormat.getNumberInstance();

   private double billAmount = 0.0; // bill amount entered by the user
   private double customPercent = 0.18; // initial custom tip percentage
   
   private int partySize = 1; // initial amount for party
   
   private TextView amountDisplayTextView; // shows formatted bill amount
   private TextView percentCustomTextView; // shows custom tip percentage
   private TextView tip15TextView; // shows 15% tip
   private TextView total15TextView; // shows total with 15% tip
   private TextView tipCustomTextView; // shows custom tip amount
   private TextView totalCustomTextView; // shows total with custom tip
   private TextView partyDisplayTextView; // shows amount of people in party

   // called when the activity is first created
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState); // call superclass's version
      setContentView(R.layout.activity_main); // inflate the GUI

      // get references to the TextViews 
      // that MainActivity interacts with programmatically
      amountDisplayTextView   =    (TextView) findViewById(R.id.amountDisplayTextView);
      percentCustomTextView   =    (TextView) findViewById(R.id.percentCustomTextView);

      partyDisplayTextView    =    (TextView) findViewById(R.id.partyDisplayTextView);

      tip15TextView           =    (TextView) findViewById(R.id.tip15TextView);
      total15TextView         =    (TextView) findViewById(R.id.total15TextView);
      tipCustomTextView       =    (TextView) findViewById(R.id.tipCustomTextView);
      totalCustomTextView     =    (TextView) findViewById(R.id.totalCustomTextView);

            
      // update GUI based on billAmount and customPercent 
      amountDisplayTextView.setText(
              currencyFormat.format(billAmount));
      
      partyDisplayTextView.setText(
              partyFormat.format(partySize));

      updateStandard(); // update the 15% tip TextViews
      updateCustom(); // update the custom tip TextViews

      // set amountEditText's TextWatcher
      EditText amountEditText = (EditText) findViewById(R.id.amountEditText);
      amountEditText.addTextChangedListener(amountEditTextWatcher);

      EditText partyEditText = (EditText) findViewById(R.id.partyEditText); 
      partyEditText.addTextChangedListener(partyEditTextWatcher); 

      
      // set customTipSeekBar's onseekBarChangeListener
      SeekBar customTipSeekBar = 
         (SeekBar) findViewById(R.id.customTipSeekBar);
      customTipSeekBar.setonseekBarChangeListener(customSeekBarListener);
   } // end method onCreate
   
   // updates 15% tip TextViews
   private void updateStandard() 
   {
      // calculate 15% tip and total
      double fifteenPercentTip = billAmount * 0.15 / partySize;
      double fifteenPercentTotal = billAmount + fifteenPercentTip / partySize;

      // display 15% tip and total formatted as currency
      tip15TextView.setText(currencyFormat.format(fifteenPercentTip));
      total15TextView.setText(currencyFormat.format(fifteenPercentTotal));
   } // end method updateStandard

   // updates the custom tip and total TextViews
   private void updateCustom() 
   {
      // show customPercent in percentCustomTextView formatted as %
      percentCustomTextView.setText(percentFormat.format(customPercent));

      // calculate the custom tip and total
      double customTip = billAmount * customPercent / partySize;
      double customTotal = billAmount + customTip / partySize;

      // display custom tip and total formatted as currency
      tipCustomTextView.setText(currencyFormat.format(customTip));
      totalCustomTextView.setText(currencyFormat.format(customTotal));
   } // end method updateCustom
   
   // called when the user changes the position of SeekBar
   private onseekBarChangeListener customSeekBarListener = 
      new onseekBarChangeListener() 
   {
      // update customPercent, then call updateCustom
      @Override
      public void onprogressChanged(SeekBar seekBar, int progress,
         boolean fromUser) 
      {
         // sets customPercent to position of the SeekBar's thumb
         customPercent = progress / 100.0;
         updateCustom(); // update the custom tip TextViews
      } // end method onprogressChanged

      @Override
      public void onstartTrackingTouch(SeekBar seekBar) 
      {
      } // end method onstartTrackingTouch

      @Override
      public void onstopTrackingTouch(SeekBar seekBar) 
      {
      } // end method onstopTrackingTouch
   }; // end onseekBarChangeListener

   // event-handling object that responds to amountEditText's events
   private TextWatcher amountEditTextWatcher = new TextWatcher() 
   {
      // called when the user enters a number
      @Override
      public void onTextChanged(CharSequence s, int start, 
         int before, int count) 
      {         
         // convert amountEditText's text to a double
         try
         {
            billAmount = Double.parseDouble(s.toString()) / 100.0;
         } // end try
         catch (NumberFormatException e)
         {
            billAmount = 0.0; // default if an exception occurs
         } // end catch 

         // display currency formatted bill amount
         amountDisplayTextView.setText(currencyFormat.format(billAmount));
         updateStandard(); // update the 15% tip TextViews
         updateCustom(); // update the custom tip TextViews
      } // end method onTextChanged

      @Override
      public void afterTextChanged(Editable s) 
      {
      } // end method afterTextChanged

      @Override
      public void beforeTextChanged(CharSequence s, int start, int count,
         int after) 
      {
      } // end method beforeTextChanged
   }; // end amountEditTextWatcher

   private TextWatcher partyEditTextWatcher = new TextWatcher()
   {
      // called when the user enters a number
      @Override
      public void onTextChanged(CharSequence s, int start,int before, int count)
      {
         partyDisplayTextView.setText(partyFormat.format(partySize));
         updateStandard(); // update the 15% tip TextViews
         updateCustom(); // update the custom tip TextViews
      } // end method onTextChanged

      @Override
      public void afterTextChanged(Editable s)
      {
      } // end method afterTextChanged

      @Override
      public void beforeTextChanged(CharSequence s, int start, int count,
                                    int after)
      {
      } // end method beforeTextChanged
   };
} // end class MainActivity




Is This A Good Question/Topic? 0
  • +

Replies To: Why can't user edit the editText?

#2 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: Why can't user edit the editText?

Posted 12 November 2015 - 02:23 PM

You are talking about this part of your code?

 private TextWatcher partyEditTextWatcher = new TextWatcher()
   {
      // called when the user enters a number
      @Override
      public void onTextChanged(CharSequence s, int start,int before, int count)
      {
         partyDisplayTextView.setText(partyFormat.format(partySize));
         updateStandard(); // update the 15% tip TextViews
         updateCustom(); // update the custom tip TextViews
      } // end method onTextChanged


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1