1. Download our Official Android App: Forums for Android!

Apps EditText/Button Issue

Discussion in 'Android Development' started by poxkix, Jul 9, 2011.

  1. poxkix

    poxkix Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Jul 9, 2011

    Jul 9, 2011
    4
    0
    5
    I made simple android ativity consisting of 1 EditText and 1 Button.

    The EditText will serve as the output when the Button is pressed.
    My expected output is when I press the Button 3 times, in the EditText it should show 3 values but no matter how I press the Button it will only show 1 value. What is wrong with my code? I can't seem to find a solution.

    XML code:
    Code (Text):
    1.  
    2. <Button[INDENT] android:layout_width="45dp"
    3. android:layout_height="45dp"
    4. android:text="@string/button_17"
    5. android:id="@+id/aye"
    6. android:textColor="@color/BLUE"
    7. android:textSize="12dp"
    8. android: onClick="onClickOne"
    9. [/INDENT]/>
    10.  
    JAVA code:
    Code (Text):
    1.  
    2. package com.android.example;
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.widget.EditText;
    7. public class example extends Activity {
    8. private EditText output;
    9. @Override
    10. public void onCreate(Bundle savedInstanceState) {
    11. super.onCreate(savedInstanceState);
    12. setContentView(R.layout.main);
    13. output = (EditText)findViewById(R.id.output);
    14. }[INDENT] public void onClickOne(View v) {
    15. output.setText(R.string.button_17);
    16. [/INDENT]}
    17. }
    18.  
     

    Advertisement

  2. miXer

    miXer Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    First of all, if this is all your code, you have not added a click listener to your button.
    Second, you don't temporerily save the EditText text you overwrite it with new text.

    Try this:
    Code (Text):
    1.  
    2. package com.android.example;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.view.View.OnClickListener;
    8. import android.widget.Button;
    9. import android.widget.EditText;
    10.  
    11. public class example extends Activity implements OnClickListener
    12. {
    13. private Button btn;
    14. private EditText output;
    15.  
    16. @Override
    17. public void onCreate(Bundle savedInstanceState)
    18. {
    19. super.onCreate(savedInstanceState);
    20. setContentView(R.layout.main);
    21.  
    22. btn = (Button)findViewById(R.id.onClickOne);
    23. btn.setOnClickListener(this);
    24. output = (EditText)findViewById(R.id.output);
    25. }
    26.  
    27. public void onClick(View v)
    28. {
    29. output.setText(output.getText().toString() + getString(R.string.button_17));
    30. }
    31. }
    32.  
     
    poxkix likes this.
  3. poxkix

    poxkix Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Jul 9, 2011

    Jul 9, 2011
    4
    0
    5
    of course! This is my code :))

    I thought the android: onClick="onClickOne" works as the click listener. So i'm wrong? Should I remove that?

    thank you for the help, I will try this and get back here.
     
  4. miXer

    miXer Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    I'm sorry. My bad. Yes, the onClick:eek:nClickOne is the click listener.
    So the main reason you don't get the ouput you want is because you dont save the current text. This should do it for you: output.setText(output.getText().toString() + getString(R.string.button_17));
     
  5. poxkix

    poxkix Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Jul 9, 2011

    Jul 9, 2011
    4
    0
    5
    thank you so much. :)

    btw, I set the EditText text default to "0.", now when I click the button the "0." will just append to the output. How do make it clear?
    Code (Text):
    1.  
    2. <EditText
    3.         android:layout_width="280dp"
    4.         android:layout_height="35dp"
    5.         android:id="@+id/output"
    6.        [COLOR=Red] android:text="0."[/COLOR]
    7.         android:gravity="right"
    8.         android:layout_gravity="center_horizontal"
    9.         android:background="@color/WHITE"
    10.         android:paddingTop="10dp"
    11.         android:layout_marginTop="5dp"
    12.         android:maxLength="25"
    13.         android:singleLine="true"
    14.         android:digits="1234567890"
    15.     />
    EDIT:
    I thought it is because of the cursor position so I tried this:

    output.getText().insert(output.getSelectionStart(), "1");

    The cursor got transferred but the default text "0." won't clear.
     

Share This Page

Loading...