Android 使用 edittext 从数组中获取特定元素

Android getting particular element from arra using edittext

我在 xml 中有数组,我正在尝试使用用户输入的数字从数组中获取特定元素,但单击按钮时我的应用程序崩溃了。

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String [] tabela;
TextView textView;
TextView textView2;
TextView textView3;
Button next, prev;
int index;
SharedPreferences sPref;
SharedPreferences.Editor editor;


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

    sPref = getSharedPreferences("daniel.myapplication", Context.MODE_PRIVATE);
    editor = sPref.edit();


    // Importing the string array from Valuses folder
    tabela = getResources().getStringArray(R.array.array);

    // initialization of textview
    textView =(TextView)findViewById(R.id.textView);
    textView2 =(TextView)findViewById(R.id.textView2);
    textView3 =(TextView)findViewById(R.id.textView3);

    //Initialization of buttons
    next =(Button)findViewById(R.id.button);
    prev =(Button)findViewById(R.id.button2);

    //OnClickListener for buttons
    next.setOnClickListener(this);
    prev.setOnClickListener(this);

    // Setting values for  variable and textviews
    index = sPref.getInt("key", 0);
    textView.setText(tabela[index]);
    textView2.setText(String.valueOf(index+1));
    textView3.setText("/"+String.valueOf(tabela.length));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onPause(){
    super.onPause();
    editor.putInt("key",index);
    editor.commit();
}

@Override
public void onClick(View v) {

    switch (v.getId()){
        case R.id.button2:
            index++;

            if (index==tabela.length){

                index=0;
                textView.setText(tabela[index]);
                textView2.setText(String.valueOf(index+1));
            }else {
                textView.setText(tabela[index]);
                textView2.setText(String.valueOf(index + 1));

            }
            break;

        case R.id.button:
            index--;


            if (index ==-1){

                index = tabela.length -1;
                textView.setText(tabela[index]);
                textView2.setText(String.valueOf(index+1));

            }else {
                textView.setText(tabela[index]);
                textView2.setText(String.valueOf(index + 1));

            }
            break;
    }

}

public void random(View view) {
    Random losuj = new Random();
    int i = losuj.nextInt(tabela.length);
    textView.setText(tabela[i]);
    textView2.setText(String.valueOf(i + 1));
}

public void press(View view) {
   EditText editText = (EditText) findViewById(R.id.editText);
    int b = Integer.parseInt(editText.getText().toString());
   // b = (int) Array.get(tabela, b);
    textView.setText(tabela[b]);
    textView2.setText(String.valueOf(b + 1));

    }

}

这是我的 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="84dp"
    android:textSize="25sp"
    android:id="@+id/textView" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/previous"
    android:id="@+id/button"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/next"
    android:id="@+id/button2"
    android:layout_alignBottom="@+id/button"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />



<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/xx"
        android:id="@+id/textView2"
        android:textSize="20sp"

        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/yy"
        android:id="@+id/textView3"
        android:textSize="20sp"

        />

</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/rand"
    android:id="@+id/button3"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:onClick="random"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button4"
    android:layout_below="@+id/button3"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="78dp"
    android:onClick="press"/>

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_alignTop="@+id/button4"
    android:layout_toRightOf="@+id/button4"
    android:onClick="press"/>

为了从资源中获取数组,请在 onCreate():

的某处执行此操作
ArrayList<String> yourArray;
yourArray = getResources().getStringArray(R.array.your_array_name_in_xml);

之后,您只需调用 yourArray.get(index) 即可获取其内容。

鉴于此,您可以这样做以获得您想要的:

int index = Integer.parseInt(yourEditText.getText().toString());
String textFromArray = yourArray.get(index);

yourTextView.setText(textFromArray);
int[] myArray = getResources().getIntArray(R.array.my_xml_int_array);
EditText editText = (EditText) findViewById(R.id.editText);
int b = Integer.valueOf(editText.getText().toString());
// Check the number is within range
if (b >= 0 && b < (myArray.length - 1)) {
    textView.setText(String.valueOf(myArray[b]));
}

您可能还应该确保在布局 xml 中将 EditText 的输入类型设置为 number

<EditText
    android:id="@+id/editText"
    ...
    android:inputType="number"/>

补充:

根据我在你的问题下的评论 "remove onClick from the EditText"。
您的最终 EditText 定义应如下所示:

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/button4"
    android:layout_toRightOf="@+id/button4"
    android:inputType="number"/>

最重要的是从 EditText 中删除 android:onClick="press"

当你 运行 它时,在编辑文本中输入你的号码,然后按 button4(你已将其标记为 "New Button")。然后所需的字符串将正确显示在 TextView.

我已经测试了您的代码,copy/pasting 完全照原样,只是按照描述更改了 `EditText',并在 xml 中创建了一个虚拟字符串数组用于测试。它工作正常。如果您仍然遇到问题,请提供更多关于正在发生的事情的描述。