0

In my activity I have the following views

TextView player1;
TextView player2;
TextView player3;
TextView player4;
EditText player1name;
EditText player2name;
EditText player3name;
EditText player4name;

Each of the TextView's has the onclick listener applied to it. and so fires the OnClick function.

When we get to the onClick this is what i am currently doing:

@Override
public void onClick(View v) {
 //the v variable is the clicked textview, in this case "player1"
 //hide the textview and show the resultant edittext
 v.setVisibility(View.GONE);
 player1name.setVisibility(View.VISIBLE);
 //set focus on edit text and when focus is lost hide it and set the textview text
 player1name.requestFocus(); 
 imm.showSoftInput(player1name, InputMethodManager.SHOW_FORCED);
 player1name.setOnFocusChangeListener(new OnFocusChangeListener() {
 @Override
 public void onFocusChange(View y, boolean x) {
 v.setVisibility(View.VISIBLE);
 player1name.setVisibility(View.GONE);
 imm.hideSoftInputFromWindow(player1name.getWindowToken(), 0);
 String name = player1name.getText().toString();
 if (name.equals("")) {
 v.setText("Player Name1");
 } else {
 v.setText(name);
 } 
 }
 });
}

However with this solution I will need to duplicate this code and change the view names for player2 - player2name, player3 - player3name etc

i can obviously grab the clicked TextView via v, however what i cant seem to do is grab its corresponding EditText.

i had thought of doing this:

View test = v + "name";
//then i replace all references to player1name with the test variable

but it doesnt work it wants me to convert View test; into a string

any suggestions?

EDIT: made it easier to understand my question

asked Feb 8, 2014 at 11:04

2 Answers 2

1
View test = v + "name";

will give a compile error. Because "v" is not a string type. and also even if it was String, test is not. This line is pretty wrong.

There a few options to achieve what you want,

You can use hashmap

Declare a global field for hashmap

 private final HashMap<Integer,EditText> map = new HashMap<Integer,EditText>();

and in onCreate method put your textview id as key, and put your edittext variables in value.

 player1name = (EditText) findViewById(R.id.player1name);
 map.put(R.id.textView1, player1name);
 // for the rest

in onClick method

EditText e = map.get(v.getId());

Then replace them with "e"

e.requestFocus(); //example
answered Feb 8, 2014 at 11:42
Sign up to request clarification or add additional context in comments.

3 Comments

This worked perfectly thanks. Except I cant pass either v or e into my onFocusChangeListener ... Any way of doing this?
what do you mean by pass?
nevermind, it was asking for a final variable inside the listener and i also had to add a cast to the TextView and EditText for the set and get Text. sorted it now thanks for your help
0

Will you please state your problem clearly? Currently, your language is very ambiguous and I can not figure out, exactly what are you looking for. It will help us to know your problem and in turn solve it.

answered Feb 8, 2014 at 11:18

2 Comments

this answer should be comment, not an answer.
@nr4bt Currently, I don't have enough privileges to comment on the question(it requires reputation 50, at least). So it was the only option to comment as an answer. Moreover, the aim is to solve the problem. So, I hope you will think about undoing vote down to my answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.