-9

I am currently working on a simple hang man application as my first experience in Android dev. I am having a little problem taking in the user's guessed word. My current approach is for the user to enter their guess in an EditText field and then press a button that assigns it to a variable called userGuess. Here is the relevant part:

public void onClick(View view){
    EditText word;
    TextView myTxt = (TextView) findViewById(R.id.display_word);
    word = (EditText) findViewById(R.id.letter_guessed);
    userGuess = word.toString();
    System.out.println(userGuess);
}

However, when I print its value, I am greeted with the following:

System.out﹕ android.widget.EditText{23b92b5a VFED..CL .F...... 48,1064-213,1188 #7f080042 app:id/letter_guessed}

I believe that my problem lies in the casting of the word and am not sure if I am taking the correct approach.

nconway
  • 11
  • 4
  • 1
    [This](https://www.google.co.in/search?q=Taking+user+input+via+EditText&ie=utf-8&oe=utf-8&gws_rd=cr&ei=P9taVcOaO5eUuATK7oDACw) is Google search result when you type your question on Google. – Kunu May 19 '15 at 06:44
  • 2
    While good questions remain unanswered, 7 people chose to answer this joke of a question, an obvious duplicate with a score of -8. Rep hunters are out in numbers today. Sigh ... – 2Dee May 19 '15 at 07:49

6 Answers6

2

Simply just do this

public void onClick(View view){
    EditText word;
    TextView myTxt = (TextView) findViewById(R.id.display_word);
    word = (EditText) findViewById(R.id.letter_guessed);
    userGuess = word.getText().toString();
    System.out.println(userGuess);
}
Quick learner
  • 10,632
  • 4
  • 45
  • 55
1

You should use getText() to retrieve text from EditText.

word.getText().toString();
Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
0

You should use word.getText().toString();

Piyush
  • 18,895
  • 5
  • 32
  • 63
Mithun
  • 2,075
  • 3
  • 19
  • 26
0

Instead of this code :

public void onClick(View view){
    EditText word;
    TextView myTxt = (TextView) findViewById(R.id.display_word);
    word = (EditText) findViewById(R.id.letter_guessed);
    userGuess = word.toString();
    System.out.println(userGuess);
}

Try using this:

EditText word= (EditText) findViewById(R.id.letter_guessed);
TextView myTxt = (TextView) findViewById(R.id.display_word);
String userGuess;

public void onClick(View view){

    userGuess = word.getText().toString();
    System.out.println(userGuess);
}
Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
0

getText() is used to input from editText.

userGuess = word.getText().toString().Trim();
Sam
  • 1,237
  • 1
  • 16
  • 29
0

It can be solved by little changes in your code. Try:

userGuess = word.getText().toString();
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
Divya Jain
  • 393
  • 1
  • 6
  • 22