Can't get String which is entered into Edit Te

2019-08-08 10:05发布

问题:

Hey so basically I have a Sign-in/Register screen for an app and I have to make it using an ExpandableListView as shown.

Based on the position of the child in a group I have inflated different views. So for positions 0 and 1, I have inflated 2 different EditText boxes.

My problem is I can't get the text that is typed in both the boxes.

The box allows me to type continuously but when I click the Sign-In button, the strings I get from the username and password boxes using getText are both null!

Sign-In screen shot http://img801.imageshack.us/img801/5544/shot000005w.png Username Toast is blank http://img21.imageshack.us/img21/3825/shot000006y.png Password Toast is blank http://img542.imageshack.us/img542/8804/shot000007.png

Here's the get getChildView from the CustomExpandableAdapter:

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
   v = null;

   position = 0;
   position=getChildId(groupPosition, childPosition);

   if(position==0) //draw the username editbox
   {
    v = inflater.inflate(R.layout.username, parent, false);
    Element c = (Element)getChild( groupPosition, childPosition );
    username = (EditText)v.findViewById( R.id.username);

    if( username != null )          
         username.setHint(c.getElement());

    }
   else if(position==1)
   {
       if(convertView == inflater.inflate(R.layout.password, parent, false))
        return convertView;
       v = inflater.inflate(R.layout.password, parent, false);
       convertView = inflater.inflate(R.layout.password, parent, false);
        Element c = (Element)getChild( groupPosition, childPosition );
        password = (EditText)v.findViewById( R.id.password);
    if( password != null )          
         password.setHint(c.getElement());

   }
   else if (position>1023 && position<1028)
   {
        v = inflater.inflate(R.layout.child_row, parent, false);
        Element c = (Element)getChild( groupPosition, childPosition );
        et = (EditText)v.findViewById( R.id.et);


    if( et != null )            
         et.setHint(c.getElement());
   }

   else if(position==1028)
    {

        v = inflater.inflate(R.layout.gender, parent, false);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
          context, R.array.gender, android.R.layout.simple_spinner_item );
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
        s = (Spinner) v.findViewById( R.id.spinner_gend );
        s.setAdapter( adapter );
    }

   else if(position==2)  //Forgot Button
   {
       v = inflater.inflate(R.layout.forgot, parent, false);
       Button forgot = (Button)v.findViewById(R.id.fpb);
       forgot.setOnClickListener(this);


   }


   else if(position==3)  //Sign-in Button
   {
       v = inflater.inflate(R.layout.sign_in_button, parent, false);
       ImageButton sign_in = (ImageButton)v.findViewById(R.id.sign_in_button);
       sign_in.setOnClickListener(this); 

}
   return v;
}

and later on in the code I have the onClick of the Sign in button:

case R.id.sign_in_button: {
        String user_name = username.getText().toString();
        String pass_word=password.getText().toString();
        Toast.makeText(context,"u:"+ user_name, 3000).show();
        Toast.makeText(context, "p:"+pass_word, 3000).show();
                    //Other unrelated Database Code


    }

Here the Toasts always show just u: and p:

Someone help!

回答1:

I just used a TextWatcher(which didn't work properly before this) The code is here:

@Override
public void afterTextChanged(Editable s) {

    user_name=s.toString();
}
@Override
public void beforeTextChanged(CharSequence s, int start,
        int count, int after) {
    // TODO Auto-generated method stub

}
@Override
public void onTextChanged(CharSequence s, int start, int before,
        int count) {
    // TODO Auto-generated method stub


}