简体   繁体   English

将TextView设置为Array值

[英]Setting TextView to Array value

I'm trying to make a small app that takes an input in an EditText number and takes the corresponding value from an array and displays it in a TextView 我正在尝试制作一个小型应用,该应用在EditText数字中接受输入,并从数组中获取相应的值,并将其显示在TextView中

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

    final EditText e = (EditText) findViewById(R.id.editText1);
    final TextView t = (TextView) findViewById(R.id.textView1);
    Button b = (Button) findViewById(R.id.button1);

    Resources res = this.getResources();

    String arr[] = getResources().getStringArray(R.array.example);

    final Editable input = e.getText();

    final String in2 = input.toString();
    int number = Integer.parseInt(in2);

    if(number < 0){
        t.setText("Input is too small");
    } else if (number > 666){
        t.setText("Input is too large");
    } else {
        final String out = arr[number].toString();
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                t.setText(in2);
            }
        }); 
    } 
}

I want it so that if the input is 0 and the button is clicked the TextView is A. 我想要它,如果输入为0并单击按钮,则TextView为A。

<string-array name="example">
    <item >A</item>
    <item >B</item>
    <item >C</item>
    <item >D</item>
</string-array>

Unfortunately whenever I start this up on an emulator or a phone it instantly crashes. 不幸的是,每当我在模拟器或电话上启动它时,它立即崩溃。 Does anyone know what I'm doing wrong? 有人知道我在做什么错吗?

The problem is that when you start your app, your editText is empty. 问题是,当您启动应用程序时, editText为空。

So in2 equals to "" , and hence you can't parse this value to an Integer. 因此in2等于"" ,因此您无法将此值解析为Integer。

Move it into your onClick method: 将其移到您的onClick方法中:

b.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {                
        int number = Integer.parseInt(e.getText().toString());
        if(number < 0){
            t.setText("Input is too small");
        } else if (number >= arr.length){
            t.setText("Input is too large");
        } else {
            t.setText(arr[number]);
        }
    }); 
} 

Okay just by looking at the code, I don't think it will accomplish what you are trying to achieve! 好吧,仅通过查看代码,我认为它不会完成您想要实现的目标!

final Editable input = e.getText();

final String in2 = input.toString();
int number = Integer.parseInt(in2);

This happens in the onCreate method. 这发生在onCreate方法中。 So when the app runs, it immediately tries to extract some text, but there's nothing to extract so Integer.parseInt(in2) will throw a runtime exception. 因此,当应用程序运行时,它会立即尝试提取一些文本,但是没有要提取的内容,因此Integer.parseInt(in2)将抛出运行时异常。

This is my suggested workflow, which may involve some re-writing of your code. 这是我建议的工作流程,可能涉及一些代码重写。

In the onCreate method: 在onCreate方法中:

  1. Get all the objects you need via findViewById as you have done. 完成后,通过findViewById获取所需的所有对象。

  2. Set listeners to the editText field and the button - the listener for the editText is responsible for tracking any changes that the user types in. In the methods given by the listener, include the following code: 将侦听器设置为editText字段和按钮-editText的侦听器负责跟踪用户键入的任何更改。在侦听器给出的方法中,包括以下代码:

    stringFromEditText = editText.getText().toString();

    Of course you should have declared these earlier on to make that work: 当然,您应该早些声明这些内容以使其起作用:

    String stringFromEditText; 字符串stringFromEditText;

    EditText editText; EditText editText;

After that, you can just call an updateTextView() method (which you define on your own). 之后,您可以调用一个updateTextView()方法(您自己定义)。 You don't even need a button! 您甚至都不需要按钮! Everything is done within the listener's methods (eg onTextChanged ) 一切都在侦听器的方法中完成(例如onTextChanged

In the updateTextView() method, don't forget to put some logic in that deals with bad inputs or null inputs: when you encounter bad inputs, simply set the value of stringFromEditText to "0". updateTextView()方法中,不要忘记添加一些逻辑来处理错误的输入或空输入:当遇到错误的输入时,只需将stringFromEditText的值设置为“ 0”即可。 So you would always succeed in parsing it in later -> no more crashes! 因此,您以后总是可以成功解析它->不再崩溃!

I know I haven't been too detailed by providing all the code. 我知道提供所有代码并没有太详细。 But I hope this helps. 但我希望这会有所帮助。

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

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