简体   繁体   English

onClick:Android Studio中的EditText为空

[英]onClick : EditText empty in android studio

I have the most simple code and it have worked before but now for some reason it doesn't and I can't figure out why! 我有最简单的代码,它以前曾经工作过,但现在由于某种原因,它不能工作,我不知道为什么! And the problem is quite hard to google. 这个问题很难用谷歌搜索。

I am unable to get the text from the EditText . 我无法从EditText获取文本。 I've created a new project, I've copied the exact code from another project (where it works) but here it just won't work. 我创建了一个新项目,已经从另一个项目中复制了确切的代码(在该项目中可以使用),但是在这里它不起作用。 I'm hoping the problem is really obvious and i've just been staring at it for too long. 我希望问题确实很明显,而且我盯着它看了太久了。

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.EditText;

    public class MainActivity extends Activity {
    String artist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText edit = (EditText) findViewById(R.id.artistEditText);
        artist = edit.getText().toString();

    }

    protected void tryLetter(View v){
        System.out.println(artist);
        Log.d("string", artist);
        System.out.println("hi");
    }
}

The println to print artist doesn't show at all and neither does the Log.d and tryletter is called when I press a button. 当我按下一个按钮时,打印艺术家的println根本不会显示,并且Log.dtryletter也不会被调用。

Here is the layout file if it makes a difference. 如果有区别,请参见以下布局文件。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Ange en artist:"
    android:id="@+id/artistTextLabel"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="30dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/artistEditText"
    android:layout_alignBottom="@+id/artistTextLabel"
    android:layout_alignEnd="@+id/searchBtn" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sök"
    android:id="@+id/searchBtn"
    android:onClick="tryLetter"
    android:layout_below="@+id/artistEditText"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

put this artist = edit.getText().toString(); 把这位artist = edit.getText().toString(); inside this tryLetter because you need to fetch the text from EditText when action is performed mean onClick and make your tryLetter method public 在该tryLetter因为执行操作时需要从EditText提取文本,这意味着onClick并将tryLetter方法public

because it will only work when your method is public 因为它仅在您的方法public时才起作用

so use this 所以用这个

public void tryLetter(View v){
    artist = edit.getText().toString();
    System.out.println(artist);
    Log.d("string", artist);
    System.out.println("hi");
}

instead of this protected void tryLetter(View v){ 而不是protected void tryLetter(View v){

In simple scenarios like yours, something needs to trigger to get the data from the EditText. 在像您这样的简单场景中,需要触发一些操作才能从EditText获取数据。 The best would be to place the fetch code, inside a trigger say A Button's block of code or inside a listener 最好的方法是将获取代码放置在触发器中,比如说一个Button的代码块中,或者放在一个监听器中

In your example there are two mistakes: 在您的示例中,有两个错误:

  1. Method tryLetter has protected modifier. 方法tryLetter具有保护修饰符。 that will not make this 那不会使这个
    method visible to layout. 布局可见的方法。 and you get exception : 并且您得到例外:
    IllegalStateException. IllegalStateException异常。 solution by making it public. 通过公开发布解决方案。
  2. You hold immutable value content of EditText as string inside onCreate by assigning it into artist. 您可以将EditText的不可变值内容作为字符串保存在onCreate中,方法是将其分配给artist。 it's empty by default and wont be change later when you insert content to EditText. 默认情况下为空,以后在将内容插入EditText时将不会更改。 Solution will be to hold instance of 解决方案将是持有
    EditText and extract String from it for each click dynamically. EditText并从中为每次单击动态提取String。

Here is your code with two fixes: 这是带有两个修复程序的代码:

import android.os.Bundle; 导入android.os.Bundle; import android.support.v7.app.AppCompatActivity; 导入android.support.v7.app.AppCompatActivity; import android.util.Log; 导入android.util.Log; import android.view.View; 导入android.view.View; import android.widget.EditText; 导入android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private EditText edit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         edit = (EditText) findViewById(R.id.artistEditText);
    }

    public void tryLetter(View v){
        String artist=edit.getText().toString();
        System.out.println(artist);
        Log.d("string", artist);
        System.out.println("hi");
    }
}

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

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