简体   繁体   English

从 EditText 中检索文本

[英]Retrieving the text from an EditText

So I am making a little app where it takes a comma separated list and returns a random word in that list and I am having trouble getting the text from the EditText view.所以我正在制作一个小应用程序,它需要一个逗号分隔的列表并在该列表中返回一个随机单词,但我无法从EditText视图中获取文本。

I saw on Stackoverflow that you're suppose to use the EditText.getText().toString() methods in order to get the text from the EditText .我在 Stackoverflow 上看到你应该使用EditText.getText().toString()方法来从EditText获取文本。 However, when I use those method, the app stops working.但是,当我使用这些方法时,应用程序停止工作。 I ran the debugger and my Input variable refers to an empty string and I also get an error that there is no local submit variable.我运行了调试器,我的 Input 变量引用了一个空字符串,我还收到一个错误,提示没有本地提交变量。

This is the java file:这是java文件:

public class MainActivity extends AppCompatActivity {
    EditText UserInput;
    Button reset;
    Button Submit;
    TextView result;
    String Input;
    String Result;
    String word;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        UserInput = (EditText) findViewById(R.id.UserInput);
        reset = (Button) findViewById(R.id.Reset);
        Submit = (Button) findViewById(R.id.Choose);
        result = (TextView) findViewById(R.id.Result);
        Input = UserInput.getText().toString();
        Result = "Your choice should be:";

        Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Random random = new Random();
            String[] List = Input.split(",");
            int num = random.nextInt(List.length - 1);
            word = List[num];
            Result = Result + word;
            result.setText(Result);
        }
    });
}

This is the EditText view of the activity:这是活动的EditText视图:

<EditText
    android:id="@+id/UserInput"
    android:layout_width="273dp"
    android:layout_height="59dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="36dp"
    android:layout_marginEnd="8dp"
    android:ems="10"
    android:hint="Choice A, Choice B, Choice C,..."
    android:inputType="text"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.515"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/Instructions" />

For example, if I write in the EditText 'a,b', I expect Input to be 'a,b' but it is not.例如,如果我在EditText写入 'a,b',我希望 Input 是 'a,b' 但它不是。 When I was debugging, it showed me it was an empty string and I do not know why.当我调试时,它告诉我它是一个空字符串,我不知道为什么。 Also.还。 I got another error that there is no local submit variable in the onClick method.我收到另一个错误,即onClick方法中没有本地提交变量。 How do I fix these errors?如何修复这些错误?

You are getting the text from EditText at the time of declaring because of which Input is null.您在声明时从EditText获取文本,因为Input为 null。 Input is not watching the edittext for change in its content. Input不会观察编辑文本的内容变化。

To do this, you have to get Input again, just before submitting.为此,您必须在提交之前再次获取 Input。

Submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Random random = new Random();
    Input = UserInput.getText().toString();
    if (Input != null) {
        String[] List = Input.split(",");
        int num = random.nextInt(List.length - 1);
        word = List[num];
        Result = Result + word;
        result.setText(Result);
    }        
    }
});

Move Input = UserInput.getText().toString();移动Input = UserInput.getText().toString(); inside the onClick method.onClick方法中。

Submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Input = UserInput.getText().toString();
        Random random = new Random();
        String[] List = Input.split(",");
        int num = random.nextInt(List.length - 1);
        word = List[num];
        Result = Result + word;
        result.setText(Result);
    }
}

You need to read the EditText only when the button is clicked, not when the app first starts.您只需要在单击按钮时读取EditText ,而不是在应用程序首次启动时读取。

Layman's explanation: In your current code, you read the EditText when the app is launched.外行的解释:在您当前的代码中,您在启动应用程序时读取了EditText Since it is empty at the time, you get an empty string when you read it.由于当时它是空的,所以当你阅读它时你会得到一个空字符串。

Debugger shows empty string in such case, num may become 0, which then can cause IndexOutOfBounds Exception.在这种情况下,调试器显示空字符串num可能变为 0,这会导致IndexOutOfBounds异常。

String[] List = Input.split(",");
int num = random.nextInt(List.length - 1);
word = List[num];

Post your logcat for further assistance.发布您的 logcat 以获得进一步的帮助。

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

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