ANDROID:在一个textView中的多对字符串之间导航

闪存卡应用程序

我有多个静态字符串存储在strings.xml上,我想使用prev_Clicked / next_Clicked按钮在它们之间导航。

我还希望能够在应用程序运行时存储问题和答案的字符串,并通过prev / next按钮访问这些字符串

感谢您的时间,请关注我的代码:


的.java

public class MainActivity extends splashActivity { Button closeButton, addButton, prevButton, nextButton; TextView QAText, answerText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_flashcard); QAText = (TextView) findViewById(R.id.fcfront_textView); answerText = (TextView) findViewById(R.id.fcfront_textView); //not used anywhere atm QAText.setOnClickListener(new View.OnClickListener() { //function to toggle between Q/A public void onClick(View v) { if(QAText.getText().toString().equals(getString(R.string.fc_front))){ QAText.setText(R.string.fc_back); Toast.makeText(getApplicationContext(), R.string.fc_front , Toast.LENGTH_LONG).show(); }else{ QAText.setText(R.string.fc_front); Toast.makeText(getApplicationContext(), "SWAPPING: back-to-front", Toast.LENGTH_LONG).show(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.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(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } //gets click event directly from xml public void next_Clicked(View view) { //test: Toast.makeText(getApplicationContext(), "Next button works!!", Toast.LENGTH_LONG).show(); } public void prev_Clicked(View view) { //test: Toast.makeText(getApplicationContext(), "Prev button works!!", Toast.LENGTH_LONG).show(); } } //end of main 

layout.xml

   <!--            

strings.xml中

   CSCE305HW1 FLASHCARD: FRONT FLASHCARD: BACK Question1 Answer1 Question2 Answer2  Nothing here yet MainActivity goodbye Next Previous  

将您的字符串定义为xml的数组,如下所示:

  string1 string1  

并在代码中将它们用作数组,如下所示:

 String[] some_array = getResources().getStringArray(R.array.your_string_array); int index = 0 ; ... prevButton.setOnClickListener(this); nextButton.setOnClickListener(this); ... @Override public void onClick(View v) { switch(v.getId) { case R.id.your_next_button_id : index++; break; case R.id.your_previous_button_id : index--; break; } yourTextView.setText(some_array[index]); } 

将字符串放在一个数组中。 您将能够使用数组索引向后导航,转发或直接跳转到给定字符串。