简体   繁体   English

没有从EditText得到任何东西

[英]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). 嗨,我真的是Android开发的新手,我只是在开发一个应用程序,将takes(在EDIT-TEXT中)输入(整数)转换为厘米并显示为普通文本(在Text-view中)。 But what ever i entered into Edit-Text I am unable to retrieve it i am posting my xml and java code: 但是,无论我输入到Edit-Text的什么内容,我都无法检索到它,而我正在发布xml和Java代码:

    <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: * * Java代码: *

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. 我尝试使用“ system.out.println”来跟踪值,但没有用(在仿真器中,输入值后运行它时,它只显示“ Your Total Is:”,而没有其他显示)。在此先感谢您,请帮我解决一下这个。

Your code is correct, but displaced. 您的代码是正确的,但已被替换。

You get nothing from the EditText because you're examining its contents right after it's created. 您无法从EditText得到任何信息,因为您正在创建它后立即检查其内容。 The function is called onCreate for a reason :) 该函数被称为onCreate的原因是:)

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(); 移动text1 = edittext1.getEditableText().toString().trim(); into your onClick() method instead. 改为您的onClick()方法。

Currently you assign text1 right after your layout is loaded. 目前,您是在加载布局后立即分配text1 At this point, your EditText is empty, so it also gets an empty value. 此时,您的EditText为空,因此它也获得一个空值。 Putting it in the onClick() method will make sure it gets a value only when you click the Button. 将其放在onClick()方法中将确保仅在单击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(). 在setContentView()之后,您将获得edittext。 Move your text1 = edittext1.getEditableText().toString().trim(); 移动您的text1 = edittext1.getEditableText()。toString()。trim(); to button's onclick method. 按钮的onclick方法。 So when user types in editext clicks on the button the values is set to textview. 因此,当用户键入editext时,单击按钮会将值设置为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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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