简体   繁体   中英

Not getting anything from EditText

Hi I am really new to Android development, I am just developing an app the takes(In EDIT-TEXT) input(integers) converts to centimetersand displays as normal text(In Text-view). But what ever i entered into Edit-Text I am unable to retrieve it i am posting my xml and java code:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"  
>

    <EditText
        android:id="@+id/edittext1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"    
        android:hint="@string/Hint1"
        android:inputType="number" />

    <Button
        android:id="@+id/Convert"
        android:text="@string/Convert"
        android:layout_width="250dp"
        android:layout_height="wrap_content"        
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="30sp"
    />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Result" 
       android:textSize="30sp"
     />

</LinearLayout>

* Java-code: *

public class MainActivity extends Activity
{   
    TextView textView1 = null;
    EditText edittext1 = null;
    int no2 = 0;
    int centimeters1 = 0;
    String text1 ="ssssss";
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        edittext1 = (EditText) findViewById(R.id.edittext1);
        textView1 = (TextView) findViewById(R.id.textView1);    
        text1 = edittext1.getEditableText().toString().trim();
        System.out.print(text1);
        System.out.print(".................................................");

        centimeters1= no2* 24 ;
        Button button1 = (Button) findViewById(R.id.Convert);

        button1.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                textView1.setText("Your Total is:" + text1);
            }

        });

    }

I tried to use "system.out.println" so that I can trace the values but it didnt work(in emulator when I run this after entering values it just displays "Your Total Is:" and nothing else).Thanks in advance, please help me with this.

Your code is correct, but displaced.

You get nothing from the EditText because you're examining its contents right after it's created. The function is called onCreate for a reason :)

Move this:

text1 = edittext1.getEditableText().toString().trim();

Here:

button1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        text1 = edittext1.getEditableText().toString().trim();
        textView1.setText("Your Total is:" + text1);
    }
});

This way, you pick up the text at the moment of pushing the button, and not on creation, when it's empty.

Move text1 = edittext1.getEditableText().toString().trim(); into your onClick() method instead.

Currently you assign text1 right after your layout is loaded. At this point, your EditText is empty, so it also gets an empty value. Putting it in the onClick() method will make sure it gets a value only when you click the Button.

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            text1 = edittext1.getEditableText().toString().trim();
            textView1.setText("Your Total is:" + text1);
        }
    });
 text1 = edittext1.getEditableText().toString().trim();

将这些代码编写在OnClickListener内,就可以了。

You are getting edittext after setContentView(). Move your text1 = edittext1.getEditableText().toString().trim(); to button's onclick method. So when user types in editext clicks on the button the values is set to textview.

button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        text1 = edittext1.getEditableText().toString().trim();
        textView1.setText("Your Total is:" + text1);
    }
});

只需更改此行:

textView1.setText("Your Total is:" + edittext1.getText().toString());

Try this one and edit text values always be in string.if you want assign that edit text value to integer means you have covert string to integer(just a info to you)..here the answer for your question..

 String str = edittext.gettext.tostring;
Textview1.settext(''your total is''+str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM