开发者

Android : EditText.getText() returns "" (Empty) String on Android 1.6

I am trying to fetch开发者_运维技巧 the EditText value on click of a button.

String ETValue = ((EditText)findViewById(R.id.ETID)).getText().toString().trim();

Every things works fine on other android versions, but on 1.6 I am getting ""(Empty) String.

Whats going wrong on Android 1.6, how this is happening?

Thanks

Screen Shots:

Android : EditText.getText() returns "" (Empty) String on Android 1.6

Android : EditText.getText() returns "" (Empty) String on Android 1.6

Code Reference :

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

        <TextView 
              android:layout_marginTop="10dip" 
              android:layout_marginLeft="5dip" 
              android:text="Type you text here"  
              android:id="@+id/TextView02" 
              android:layout_height="wrap_content" 
              android:textColor="#333333" 
              android:layout_width="fill_parent" 
              android:textSize="16dip">
              </TextView>

            <LinearLayout android:orientation="horizontal"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:id="@+id/ID1">   
                <EditText android:layout_marginLeft="5dip" 
                      android:id="@+id/keyword"
                      android:hint="e.g. Text here" 
                      android:textSize="17dip"
                      android:singleLine="true" 
                      android:layout_height="wrap_content" 
                      android:layout_width="250dip"/>
                <Button android:id="@+id/btnID"
                      android:textStyle="bold" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content"
                      android:textColor="#FFFFFF"
                      android:background="@drawable/icon"
                      android:gravity="bottom"
                      android:paddingBottom="9dip"
                      android:layout_marginLeft="3dip"/>      
            </LinearLayout>

            <LinearLayout android:orientation="horizontal"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:id="@+id/ID2"
             android:visibility="gone">   
                <EditText android:layout_marginLeft="5dip" 
                      android:id="@+id/keyword"
                      android:hint="e.g. Text here"
                      android:textSize="17dip" 
                      android:singleLine="true" 
                      android:layout_height="wrap_content" 
                      android:layout_width="fill_parent"
                      android:layout_marginRight="5dip" />
            </LinearLayout>      

<Button android:text="Click" android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>

Etext.Java

public class EText extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
    ((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);

    Button but = (Button) findViewById(R.id.Button01);
    but.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            String ETValue = ((EditText) findViewById(R.id.keyword)).getText().toString().trim();
            Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
        }
    });
} }

Even this way doesn't works

public class EText extends Activity {
    /** Called when the activity is first created. */
    EditText ETextt1 = null;
    EditText ETextt2 = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if(Integer.parseInt(Build.VERSION.SDK) < 7){
            ((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
            ((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
            ETextt1 = ((EditText) findViewById(R.id.keyword1));
        }else{
            ETextt2 = ((EditText) findViewById(R.id.keyword2));
        }

        Button but = (Button) findViewById(R.id.Button01);
        but.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                String ETValue = null;
                if(null == ETextt1){
                    ETValue = ETextt2.getText().toString().trim();
                }else if(null == ETextt2){
                    ETValue = ETextt1.getText().toString().trim();
                }

                Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
            }
        });
    } }

This Works Perfectly Fine :-)

package com.test.et;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class EText extends Activity {
    /** Called when the activity is first created. */
    EditText ETextt1 = null;
    EditText ETextt2 = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if(Integer.parseInt(Build.VERSION.SDK) < 7){
            ((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
            ((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);
        }

        //ETextt = ((EditText) findViewById(R.id.keyword1));

        Button but = (Button) findViewById(R.id.Button01);
        but.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                String ETValue = null;
                if(Integer.parseInt(Build.VERSION.SDK) < 7){
                    ETValue = ((EditText) findViewById(R.id.keyword2)).getText().toString().trim();
                }else{
                    ETValue = ((EditText) findViewById(R.id.keyword1)).getText().toString().trim();
                }

                Toast.makeText(EText.this, ETValue, Toast.LENGTH_SHORT).show();
            }
        });
    } }


I have found the problems source. You declareted two EditTexts with the same id. To resolve problem just rename your EditText as keyword1 and keyword2. Then, get text from second EditText.

public class EdText extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    ((LinearLayout)findViewById(R.id.ID1)).setVisibility(View.GONE);
    ((LinearLayout)findViewById(R.id.ID2)).setVisibility(View.VISIBLE);

    Button but = (Button) findViewById(R.id.Button01);
    but.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            String ETValue = ((EditText) findViewById(R.id.keyword2)).getText().toString().trim();
            Toast.makeText(EdText.this, ETValue, Toast.LENGTH_SHORT).show();
        }
    });
} }

layout:

`

    <TextView 
          android:layout_marginTop="10dip" 
          android:layout_marginLeft="5dip" 
          android:text="Type you text here"  
          android:id="@+id/TextView02" 
          android:layout_height="wrap_content" 
          android:textColor="#333333" 
          android:layout_width="fill_parent" 
          android:textSize="16dip">
          </TextView>

        <LinearLayout android:orientation="horizontal"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/ID1">   
            <EditText android:layout_marginLeft="5dip" 
                  android:id="@+id/keyword1"
                  android:hint="e.g. Text here" 
                  android:textSize="17dip"
                  android:singleLine="true" 
                  android:layout_height="wrap_content" 
                  android:layout_width="250dip"/>
            <Button android:id="@+id/btnID"
                  android:textStyle="bold" 
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content"
                  android:textColor="#FFFFFF"
                  android:background="@drawable/icon"
                  android:gravity="bottom"
                  android:paddingBottom="9dip"
                  android:layout_marginLeft="3dip"/>      
        </LinearLayout>

        <LinearLayout android:orientation="horizontal"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:id="@+id/ID2"
         android:visibility="gone">   
            <EditText android:layout_marginLeft="5dip" 
                  android:id="@+id/keyword2"
                  android:hint="e.g. Text here"
                  android:textSize="17dip" 
                  android:singleLine="true" 
                  android:layout_height="wrap_content" 
                  android:layout_width="fill_parent"
                  android:layout_marginRight="5dip" />
        </LinearLayout>      

`


I have just finished struggling with EditText returning an empty string (""). The reason was that setContentView(R.layout.main); was invoked twice:

private EditText editText1;
private EditText editText2;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  /// !!!
    editText1=(EditText)findViewById(R.id.editText1);
    editText2=(EditText)findViewById(R.id.editText2);

    // some really long and untrivial initialization stuff

    setContentView(R.layout.main);  /// !!!
}

It looks like editText1 and editText2 pointed into the parts of the previous incarnation of R.layout.main...


Here is my test example for your case:

public class EdText extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // string initialised here will always have initial value and never changes
    final String ETValue = ((EditText) findViewById(R.id.EditText01)).getText().toString().trim();

    Button but = (Button) findViewById(R.id.Button01);
    but.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Toast.makeText(EdText.this, ((EditText) findViewById(R.id.EditText01)).getText().toString().trim(), Toast.LENGTH_SHORT).show();
        }
    });
} }

Everything works fine for API 4. Maybe your probles connected with the moment when you read the string value. see my comment.


public class EditTextDemo extends Activity { /** Called when the activity is first created. */

Button btnClick;
 String ETValue;
 @Override
    public void onCreate(Bundle savedInstanceState) 
 {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
ETValue = ((EditText)findViewById(R.id.editText1)).getText().toString().trim();
btnClick.setOnClickListener(new OnClickListener() 
{
    @Override
        public void onClick(View view) 
        {
        //assign a default value
        ETValue.equals("Welcome!!");
        Toast.makeText(getBaseContext(), "Value is:"+ETValue, Toast.LENGTH_SHORT).show();
        Log.i("EditTextDemo","-----Value of EditText is:----------"+ETValue);
        }
});

}}

You can also give value at runtime.. Also that value can be used for further coding. Hope this will help!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜