所以我有这个小代码,我在点击特定按钮指定时间后尝试显示一个数字。我已经通过条件完成了这个,但我很确定这不是最有效的方法。在我的代码中我所做的就是每次点击一个按钮时都会显示一个数字。我想要的例子是按下按钮33次后显示数字1,并在注册另外33次点击时显示数字2,等等。
package engineers.android.com.azkar;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Sebh extends Activity {
TextView textView;
TextView textView_sum;
TextView textView1;
TextView textView_sum1;
TextView textView2;
TextView textView_sum2;
Context context = this;
int coun1=0;
int coun2=0;
int coun3=0;
int sum1=0;
int sum2=0;
int sum3=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sebhah);
Button bhome = (Button) findViewById(R.id.bh);
bhome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
textView = (TextView)findViewById(R.id.c1);
textView_sum = (TextView)findViewById(R.id.s1);
textView1 = (TextView) findViewById(R.id.c2);
textView_sum1 = (TextView) findViewById(R.id.s2);
textView2 = (TextView) findViewById(R.id.c3);
textView_sum2 = (TextView) findViewById(R.id.s3);
}
public void s5(View view) {
coun1++;
textView.setText(String.valueOf(coun1));
if (coun1 == 33 ) {
sum1=1;
textView_sum.setText(String.valueOf(sum1));
}else if (coun1 == 66 ) {
sum1=2;
textView_sum.setText(String.valueOf(sum1));
}else if (coun1 == 99 ) {
sum1=3;
textView_sum.setText(String.valueOf(sum1));
}else if (coun1 == 132 ) {
sum1=4;
textView_sum.setText(String.valueOf(sum1));
}else if (coun1 == 165 ) {
sum1=5;
textView_sum.setText(String.valueOf(sum1));
}else if (coun1 == 198 ) {
sum1=6;
textView_sum.setText(String.valueOf(sum1));
}else if (coun1 == 231 ) {
sum1=7;
textView_sum.setText(String.valueOf(sum1));
}else if (coun1 == 264 ) {
sum1=8;
textView_sum.setText(String.valueOf(sum1));
}else if (coun1 == 297 ) {
sum1=9;
textView_sum.setText(String.valueOf(sum1));
}else if (coun1 == 330 ) {
sum1=10;
textView_sum.setText(String.valueOf(sum1));
}
}
public void sb(View view) {
coun2++;
textView1.setText(String.valueOf(coun2));
if (coun2 == 33 ) {
sum2=1;
textView_sum1.setText(String.valueOf(sum2));
}else if (coun2 == 66 ) {
sum2=2;
textView_sum1.setText(String.valueOf(sum2));
}else if (coun2 == 99 ) {
sum2=3;
textView_sum1.setText(String.valueOf(sum2));
}else if (coun2 == 132 ) {
sum2=4;
textView_sum1.setText(String.valueOf(sum2));
}else if (coun2 == 165 ) {
sum2=5;
textView_sum1.setText(String.valueOf(sum2));
}else if (coun2 == 198 ) {
sum2=6;
textView_sum1.setText(String.valueOf(sum2));
}else if (coun2 == 231 ) {
sum2=7;
textView_sum1.setText(String.valueOf(sum2));
}else if (coun2 == 264 ) {
sum2=8;
textView_sum1.setText(String.valueOf(sum2));
}else if (coun2 == 297 ) {
sum2=9;
textView_sum1.setText(String.valueOf(sum2));
}else if (coun2 == 330 ) {
sum2=10;
textView_sum1.setText(String.valueOf(sum2));
}
}
public void hm (View view) {
coun3++;
textView2.setText(String.valueOf(coun3));
if (coun3 == 33 ) {
sum3=1;
textView_sum2.setText(String.valueOf(sum3));
}else if (coun3 == 66 ) {
sum3=2;
textView_sum2.setText(String.valueOf(sum3));
}else if (coun3 == 99 ) {
sum3=3;
textView_sum2.setText(String.valueOf(sum3));
}else if (coun3 == 132 ) {
sum3=4;
textView_sum2.setText(String.valueOf(sum3));
}else if (coun3 == 165 ) {
sum3=5;
textView_sum2.setText(String.valueOf(sum3));
}else if (coun3 == 198 ) {
sum3=6;
textView_sum2.setText(String.valueOf(sum3));
}else if (coun3 == 231 ) {
sum3=7;
textView_sum2.setText(String.valueOf(sum3));
}else if (coun3 == 264 ) {
sum3=8;
textView_sum2.setText(String.valueOf(sum3));
}else if (coun3 == 297 ) {
sum3=9;
textView_sum2.setText(String.valueOf(sum3));
}else if (coun3 == 330 ) {
sum3=10;
textView_sum2.setText(String.valueOf(sum3));
}
}
public void stop(View view) {
coun1=0;
coun2=0;
coun3=0;
textView.setText(String.valueOf(coun1));
textView1.setText(String.valueOf(coun2));
textView2.setText(String.valueOf(coun3));
}
public void stop_sum(View view) {
sum1=0;
sum2=0;
sum3=0;
textView_sum.setText(String.valueOf(sum1));
textView_sum1.setText(String.valueOf(sum2));
textView_sum2.setText(String.valueOf(sum3));
}
}
答案 0 :(得分:0)
如果你向我展示了你可以提供按钮参考的代码,我可以提出一个比这个更好的解决方案
public void s5(View view) {
coun1++;
coun1 %= 33;
if (coun1 == 0) {
sum1++;
textView_sum.setText(String.valueOf(sum1));
}
}
public void sb(View view) {
coun2++;
coun2 %= 33;
if (coun2 == 0) {
sum2++;
textView1.setText(String.valueOf(coun2));
}
}
public void hm (View view) {
coun3++;
coun3 %= 33;
if (coun3 == 0) {
sum3++;
textView2.setText(String.valueOf(sum3));
}
}