所以我想通过按下下一个或上一个项目的按钮来按顺序在文本视图中显示字符串数组中的项目。我是编码的新手,所以这就是我设法做的。
String[] PeopleFactsArray;
Button pplnextbtn, pplprevbtn;
TextView pplfacts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.people_facts);
PeopleFactsArray=getResources().getStringArray(R.array.people_facts);
pplnextbtn=(Button) findViewById(R.id.pplnextbtn);
pplprevbtn=(Button) findViewById(R.id.pplprevbtn);
pplfacts=(TextView) findViewById(R.id.pplfacts);
pplnextbtn.setOnClickListener(new FactSelect());
pplprevbtn.setOnClickListener(new FactSelect());
int factcount=0;
pplfacts.setText(PeopleFactsArray[factcount]);
}
class FactSelect implements View.OnClickListener {
@Override
public void onClick(View v) {
TextView pplfacts = null;
int factcount=0;
int factlist=PeopleFactsArray.length;
pplfacts.setText(PeopleFactsArray[factcount]);
if (v.getId() == R.id.pplnextbtn)
{
for (factcount = 0; factcount < factlist; factcount++)
{
factcount = factcount + 1;
}
}
else if (v.getId() == R.id.pplprevbtn)
{
for (factcount = factlist; factcount > 0; factcount--)
{
factcount = factcount - 1;
}
}
}
}
我的textview如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/people_layout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/pplfacts"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/pplnextbtn"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="84dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"
android:id="@+id/pplprevbtn"
android:layout_alignTop="@+id/pplnextbtn"
android:layout_toLeftOf="@+id/pplfacts"
android:layout_toStartOf="@+id/pplfacts" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
android:id="@+id/pplsharebtn"
android:layout_alignTop="@+id/pplnextbtn"
android:layout_toRightOf="@+id/pplfacts"
android:layout_toEndOf="@+id/pplfacts" />
</RelativeLayout>
我的字符串数组如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="people_facts">
<item>people fact 1</item>
<item>people fact 2</item>
<item>people fact 3</item>
<item>people fact 4</item>
<item>people fact 5</item>
<item>people fact 6</item>
<item>people fact 7</item>
<item>people fact 8</item>
<item>people fact 9</item>
<item>people fact 10</item>
</string-array>
</resources>
当我运行它并尝试按下一个或上一个按钮时,我的应用程序崩溃了。如果有人能引导我朝着正确的方向前进,我将非常感激。谢谢。
答案 0 :(得分:0)
将onClick实现替换为:
@Override
public void onClick(View v) {
int factcount=indexOfCurrentSelection;
int factlist=PeopleFactsArray.length;
if (v.getId() == R.id.pplnextbtn)
{
if(factcount < factlist-1) factcount = factcount + 1;
}
else if (v.getId() == R.id.pplprevbtn)
{
if(factcount > 0) factcount = factcount - 1;
}
pplfacts.setText(PeopleFactsArray[factcount]);
indexOfCurrentSelection = factcount;
}
将indexOfCurrentSelection声明为全局变量&amp;将其初始化为0。