@RequiresApi(api = Build.VERSION_CODES.O)
public class MainActivity extends AppCompatActivity {
TextSwitcher textSwitcherage, textSwitcherGender;
Button nextButton, previousButton, nextButton2, previousButton2;
String[] age = {"U-4","Kid","Teen","Adult","Old"};
String[] gender = {"Male","Female","Other"};
TextView textView;
Typeface typeface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
defVars();
}
public void defVars()
{
textSwitcherGender = findViewById(R.id.textSwitcherGender);
textSwitcherage = findViewById(R.id.textSwitcherage);
nextButton = findViewById(R.id.nextButton1);
nextButton2 = findViewById(R.id.nextButton2);
previousButton = findViewById(R.id.previousButton1);
previousButton2 = findViewById(R.id.previousButton2);
typeface = getResources().getFont(R.font.lettersforlearners);
nextAndPreviousButtons(nextButton,previousButton,textSwitcherage,age,0);
nextAndPreviousButtons(nextButton2, previousButton2, textSwitcherGender, gender,0);
}
public void nextAndPreviousButtons(Button buttonNext, Button buttonPrevious, final TextSwitcher textSwitcher, final String[] data, final Integer stringIndex)
{
buttonNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(stringIndex == data.length - 1)
{
stringIndex = 0;
textSwitcher.setText(data[stringIndex]);
}
else
{
textSwitcher.setText(data[++stringIndex]);
}
}
});
buttonPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(stringIndex==0)
{
stringIndex = data.length-1;
textSwitcher.setText(data[stringIndex]);
}
else
{
textSwitcher.setText(data[--stringIndex]);
}
}
});
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
textView = new TextView(MainActivity.this);
textView.setTypeface(typeface);
textView.setTextColor(Color.BLACK);
textView.setTextSize(40);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
return textView;
}
});
textSwitcher.setText(data[stringIndex]);
}
}
我有一个错误:
“错误:无法为最终变量stringIndex赋值”
我将stringIndex
设置为final,但是仍然遇到了问题。我不知道。我是否必须为stringIndex定义Integer变量?还是应该删除stringIndex final?我需要为每个函数定义单独的stringIndex变量
答案 0 :(得分:0)
如果您声明final Integer stringIndex...
,那么您就完成了!这就是保留字final的概念和含义。。。以后是无法重新分配的对象。
public void nextAndPreviousButtons(Button buttonNext, Button buttonPrevious,
final TextSwitcher textSwitcher, final String[] data, Integer stringIndex)
答案 1 :(得分:0)
您不必修改stringIndex
public void nextAndPreviousButtons(Button buttonNext, Button buttonPrevious, final TextSwitcher textSwitcher, final String[] data, final Integer stringIndex)
{
buttonNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(stringIndex == data.length - 1)
{
textSwitcher.setText(data[0]);
}
else
{
textSwitcher.setText(data[stringIndex+1]);
}
}
});
buttonPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(stringIndex==0)
{
textSwitcher.setText(data[data.length-1]);
}
else
{
textSwitcher.setText(data[stringIndex-1]);
}
}
});
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
textView = new TextView(MainActivity.this);
textView.setTypeface(typeface);
textView.setTextColor(Color.BLACK);
textView.setTextSize(40);
textView.setGravity(Gravity.CENTER_HORIZONTAL);
return textView;
}
});
textSwitcher.setText(data[stringIndex]);