xml代码
<TextView
android:id="@+id/tvSTNN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#F2416B"
android:textSize="150sp" />
<TextView
android:id="@+id/tvSTN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvSTNN"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:text="ONE"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#F2416B"
android:textSize="100sp" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_marginTop="400dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btnRestart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restart"
/>
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
/>
<Button
android:id="@+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous"
/>
</LinearLayout>
java代码
package com.newApp.textfield;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SpellToNumbers extends Activity implements View.OnClickListener{
Button btn_Next;
Button btnRestart;
Button btn_previous;
TextView tvSTN, tvSTNN;
int a;
RelativeLayout stnXML;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spell_to_numbers);
tvSTN = (TextView) findViewById(R.id.tvSTN);
tvSTNN = (TextView) findViewById(R.id.tvSTNN);
stnXML = (RelativeLayout) findViewById(R.id.stnXML);
btn_Next = (Button) findViewById(R.id.btnNext);
btn_Next.setOnClickListener(this);
btnRestart = (Button) findViewById(R.id.btnRestart);
btnRestart.setOnClickListener(this);
btn_previous = (Button) findViewById(R.id.btnPrevious);
btn_previous.setOnClickListener(this);
}//onCreate end
@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;
}//onCreateOptionMenu end
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.btnRestart:
tvSTNN.setText("1");
tvSTN.setText("ONE");
tvSTNN.setTextColor(Color.parseColor("#F2416B"));
tvSTN.setTextColor(Color.parseColor("#F2416B"));
stnXML.setBackgroundColor(Color.parseColor("#3FD161"));
a = 0;
break;
case R.id.btnNext:
if (a == 0)
{
tvSTNN.setText("2");
tvSTNN.setTextColor(Color.parseColor("#F0EFDA"));
tvSTN.setText("TWO");
tvSTN.setTextColor(Color.parseColor("#F0EFDA"));
stnXML.setBackgroundColor(Color.parseColor("#0A0EF5"));
a = 1;
}
else if (a == 1)
{
tvSTNN.setText("3");
tvSTNN.setTextColor(Color.parseColor("#F5F507"));
tvSTN.setText("THREE");
tvSTN.setTextColor(Color.parseColor("#F5F507"));
stnXML.setBackgroundColor(Color.parseColor("#D15BE3"));
a = 2;
}
else if (a == 2)
{
tvSTNN.setText("4");
tvSTNN.setTextColor(Color.parseColor("#0AF5DD"));
tvSTN.setText("FOUR");
tvSTN.setTextColor(Color.parseColor("#0AF5DD"));
stnXML.setBackgroundColor(Color.parseColor("#F50A0A"));
a = 3;
}
else if (a == 3)
{
tvSTNN.setText("5");
tvSTNN.setTextColor(Color.parseColor("#171117"));
tvSTN.setText("FIVE");
tvSTN.setTextColor(Color.parseColor("#171117"));
stnXML.setBackgroundColor(Color.parseColor("#0AF5DD"));
a = 4;
}
else if (a == 4)
{
tvSTNN.setText("6");
tvSTNN.setTextColor(Color.parseColor("#072BF5"));
tvSTN.setText("SIX");
tvSTN.setTextColor(Color.parseColor("#072BF5"));
stnXML.setBackgroundColor(Color.parseColor("#0AF525"));
a = 5;
}
else if (a == 5)
{
tvSTNN.setText("7");
tvSTNN.setTextColor(Color.parseColor("#8F0AF5"));
tvSTN.setText("SEVEN");
tvSTN.setTextColor(Color.parseColor("#8F0AF5"));
stnXML.setBackgroundColor(Color.parseColor("#F5F10A"));
a = 6;
}
else if (a == 6)
{
tvSTNN.setText("8");
tvSTNN.setTextColor(Color.parseColor("#37F507"));
tvSTN.setText("EIGHT");
tvSTN.setTextColor(Color.parseColor("#37F507"));
stnXML.setBackgroundColor(Color.parseColor("#9C2497"));
a = 7;
}
else if (a == 7)
{
tvSTNN.setText("9");
tvSTNN.setTextColor(Color.parseColor("#F50707"));
tvSTN.setText("NINE");
tvSTN.setTextColor(Color.parseColor("#F50707"));
stnXML.setBackgroundColor(Color.parseColor("#BAF50A"));
a = 8;
}
else if (a == 8)
{
tvSTNN.setText("10");
tvSTNN.setTextColor(Color.parseColor("#0ACAF5"));
tvSTN.setText("TEN");
tvSTN.setTextColor(Color.parseColor("#0ACAF5"));
stnXML.setBackgroundColor(Color.parseColor("#F5700A"));
tvSTN.setClickable(false);
}
break;
case R.id.btnPrevious:
a--;
btn_Next.performClick();
break;
}//switch end
}//onClick end
}//class end
答案 0 :(得分:0)
首先,您尚未初始化已全局声明的变量a
的值。您似乎只在重启按钮单击时初始化了a
的值。但是,如果你只是在重启按钮点击后启动程序,那么没有问题。
但是,至少要为0
初始化a
。
答案 1 :(得分:0)
// other your code
// ...
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnRestart:
restart();
break;
case R.id.btnNext:
next();
break;
case R.id.btnPrevious:
previous();
break;
}// switch end
}// onClick end
private void restart() {
a = 0;
setPage();
}
private void previous() {
if (a > 0) {
a--;
}
setPage();
}
private void next() {
if (a < 9) {
a++;
}
setPage();
}
private void setPage() {
if (a == 0) {
tvSTNN.setText("1");
tvSTN.setText("ONE");
tvSTNN.setTextColor(Color.parseColor("#F2416B"));
tvSTN.setTextColor(Color.parseColor("#F2416B"));
stnXML.setBackgroundColor(Color.parseColor("#3FD161"));
} else if (a == 1) {
tvSTNN.setText("2");
tvSTNN.setTextColor(Color.parseColor("#F0EFDA"));
tvSTN.setText("TWO");
tvSTN.setTextColor(Color.parseColor("#F0EFDA"));
stnXML.setBackgroundColor(Color.parseColor("#0A0EF5"));
}
else if (a == 2) {
tvSTNN.setText("3");
tvSTNN.setTextColor(Color.parseColor("#F5F507"));
tvSTN.setText("THREE");
tvSTN.setTextColor(Color.parseColor("#F5F507"));
stnXML.setBackgroundColor(Color.parseColor("#D15BE3"));
} else if (a == 3) {
tvSTNN.setText("4");
tvSTNN.setTextColor(Color.parseColor("#0AF5DD"));
tvSTN.setText("FOUR");
tvSTN.setTextColor(Color.parseColor("#0AF5DD"));
stnXML.setBackgroundColor(Color.parseColor("#F50A0A"));
} else if (a == 4) {
tvSTNN.setText("5");
tvSTNN.setTextColor(Color.parseColor("#171117"));
tvSTN.setText("FIVE");
tvSTN.setTextColor(Color.parseColor("#171117"));
stnXML.setBackgroundColor(Color.parseColor("#0AF5DD"));
} else if (a == 5) {
tvSTNN.setText("6");
tvSTNN.setTextColor(Color.parseColor("#072BF5"));
tvSTN.setText("SIX");
tvSTN.setTextColor(Color.parseColor("#072BF5"));
stnXML.setBackgroundColor(Color.parseColor("#0AF525"));
} else if (a == 6) {
tvSTNN.setText("7");
tvSTNN.setTextColor(Color.parseColor("#8F0AF5"));
tvSTN.setText("SEVEN");
tvSTN.setTextColor(Color.parseColor("#8F0AF5"));
stnXML.setBackgroundColor(Color.parseColor("#F5F10A"));
} else if (a == 7) {
tvSTNN.setText("8");
tvSTNN.setTextColor(Color.parseColor("#37F507"));
tvSTN.setText("EIGHT");
tvSTN.setTextColor(Color.parseColor("#37F507"));
stnXML.setBackgroundColor(Color.parseColor("#9C2497"));
} else if (a == 8) {
tvSTNN.setText("9");
tvSTNN.setTextColor(Color.parseColor("#F50707"));
tvSTN.setText("NINE");
tvSTN.setTextColor(Color.parseColor("#F50707"));
stnXML.setBackgroundColor(Color.parseColor("#BAF50A"));
} else if (a == 9) {
tvSTNN.setText("10");
tvSTNN.setTextColor(Color.parseColor("#0ACAF5"));
tvSTN.setText("TEN");
tvSTN.setTextColor(Color.parseColor("#0ACAF5"));
stnXML.setBackgroundColor(Color.parseColor("#F5700A"));
tvSTN.setClickable(false);
}
}
你有不正确的算法,你的增量“a”变量在下一个函数中(在更新视图之后),之后你点击上一个。减少以前的状态。结果 - 你没有对下一个行动......
我修理了你的算法。