Android使用edittext从arra获取特定元素
我在xml中有一个数组,我正试图使用用户输入的数组编号从数组中获取一个特定的元素,但当单击按钮时,我的应用程序崩溃了Android使用edittext从arra获取特定元素,android,arrays,android-edittext,Android,Arrays,Android Edittext,我在xml中有一个数组,我正试图使用用户输入的数组编号从数组中获取一个特定的元素,但当单击按钮时,我的应用程序崩溃了 public class MainActivity extends AppCompatActivity implements View.OnClickListener { String [] tabela; TextView textView; TextView textView2; TextView textView3; Button next, prev; int index
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()中的某个位置执行此操作。
:
附加: 根据我在您的问题“从编辑文本中删除onClick”下的评论,
最终的
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”
运行时,在编辑文本中输入您的号码,然后按按钮4
(您已将其标记为“新建按钮”)。然后所需字符串将正确显示在文本视图中
我已经测试了您的代码,完全按照原样复制/粘贴了它,并且只按说明更改了“EditText”,并用xml创建了一个虚拟字符串数组进行测试。它工作正常。如果您仍然有问题,请提供更多的描述。同时删除()
在.length
之后。这是一个公共字段,不是一个公共方法。在按下new按钮后,使我的ap崩溃的原因是,在我的edittext中,android:layout\u width是包装内容,当我更改为匹配父项时,它工作,我不知道xml文件会导致崩溃这很奇怪,现在它也适用于你的代码和我的代码。但是我必须减少我的变量,比如索引--;,如果没有这个,它显示的数字和字符串比它应该显示的多,我必须使用1个变量作为应用程序的索引,以便在关闭应用程序后记住哪个字符串是最后一个,变量索引被分配给SharedReferences。你能解释我为什么要做索引--;,而不是+,因为我没有取消rstand为什么它只在索引--时才正确工作。我可以看到您已将代码放在一个方法中按(查看),但您在代码中的何处使用此方法?您刚刚将其指定为xml中的onClick吗?
onClick`方法会在您单击EditText
时立即启动,而不是在您输入完值后启动。是的,按钮有方法在xml中单击我是否应该删除按钮我尝试将您的xml布局添加到问题中,以便我们可以n获取完整信息。运行应用程序时,从EditText
中删除onClick
。在EditText
中键入您的号码,然后按按钮4
触发press
方法。
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]));
}
<EditText
android:id="@+id/editText"
...
android:inputType="number"/>
<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"/>