繁体   English   中英

从 EditText 中检索文本

[英]Retrieving the text from an EditText

所以我正在制作一个小应用程序,它需要一个逗号分隔的列表并在该列表中返回一个随机单词,但我无法从EditText视图中获取文本。

我在 Stackoverflow 上看到你应该使用EditText.getText().toString()方法来从EditText获取文本。 但是,当我使用这些方法时,应用程序停止工作。 我运行了调试器,我的 Input 变量引用了一个空字符串,我还收到一个错误,提示没有本地提交变量。

这是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);
        }
    });
}

这是活动的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" />

例如,如果我在EditText写入 'a,b',我希望 Input 是 'a,b' 但它不是。 当我调试时,它告诉我它是一个空字符串,我不知道为什么。 还。 我收到另一个错误,即onClick方法中没有本地提交变量。 如何修复这些错误?

您在声明时从EditText获取文本,因为Input为 null。 Input不会观察编辑文本的内容变化。

为此,您必须在提交之前再次获取 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);
    }        
    }
});

移动Input = UserInput.getText().toString(); 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);
    }
}

您只需要在单击按钮时读取EditText ,而不是在应用程序首次启动时读取。

外行的解释:在您当前的代码中,您在启动应用程序时读取了EditText 由于当时它是空的,所以当你阅读它时你会得到一个空字符串。

在这种情况下,调试器显示空字符串num可能变为 0,这会导致IndexOutOfBounds异常。

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

发布您的 logcat 以获得进一步的帮助。

暂无
暂无

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

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