Domanda

I am new to android programming. I am making a small test app which is a simple multiplier. My app has a Textview and three buttons.Namely Multiply by 2, Multiply by 3 and Clear. The code works fine while multiplication ,but I want the loop to end after 10 and the textview should display "Stopped" after the 10th click of the buttons..I have tried various techniques but none seem to work. I am not sure whether "Do-while" is a good idea. Here's the java code:

public class MainActivity extends Activity {

TextView Display;
Button X2, X3, Clear;
int a = 1, result;
String Over = "Stopped";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // a = 1;
    X2 = (Button) findViewById(R.id.bX2);
    X3 = (Button) findViewById(R.id.bX3);
    Clear = (Button) findViewById(R.id.bClear);
    Display = (TextView) findViewById(R.id.tvDisplay);
    X2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // a = 1;
            // this loop should stop after the 10th click of the button
            // and the text view should display "stopped"
            do {
                result = a * 2;
                Display.setText("2 X " + a + " = " + result);                   
                a++;                    
            } while (a == 0);
        }
    });
    X3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // a = 1;
            // this loop should stop after the 10th click of the button
            // and the text view should display "stopped"
            do {
                result = a * 3;
                Display.setText("3 X " + a + " = " + result);                   
                a++;                    
            } while (a == 0);
        }
    });
    Clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            result = 0;
            a = 1;
            Display.setText("" + result);
        }
    });
}

}

and the here's the XML code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="clip_vertical"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tvDisplay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:hint="Result display"
    android:textSize="25sp" />

<Button
    android:layout_width="250dp"
    android:layout_gravity="center"
    android:id="@+id/bX2"
    android:layout_height="wrap_content"
    android:text="Multiply by 2" />

<Button
    android:layout_width="250dp"
    android:layout_gravity="center"
    android:id="@+id/bX3"
    android:layout_height="wrap_content"
    android:text="Multiply by 3" />

<Button
    android:layout_width="75dp"
    android:layout_gravity="center"
    android:id="@+id/bClear"
    android:layout_height="wrap_content"
    android:text="Clear" />

I am not aware of the posting rules and hence excuse me if I am violating any. I am really stuck here, and any help would be "Great".

Thank you.

È stato utile?

Soluzione 4

public class MainActivity extends Activity {

TextView Display;
Button X2, X3, Clear;
int a = 1, result;
String Over = "Stopped";
counter = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // a = 1;
    X2 = (Button) findViewById(R.id.bX2);
    X3 = (Button) findViewById(R.id.bX3);
    Clear = (Button) findViewById(R.id.bClear);
    Display = (TextView) findViewById(R.id.tvDisplay);
    X2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // a = 1;
            // this loop should stop after the 10th click of the button
            // and the text view should display "stopped"
            if(counter<10)

                result = a * 2;
                Display.setText("2 X " + a + " = " + result);                   
                a++;  
               counter++                  
            } 
           else{
                     Display.setText("Stopped");   
               }
        }
    });

Altri suggerimenti

Your loop is executing for each click

A do-while always executes once before checking the while condition, also I can't see any logic related to the value 10.

You probably want

if (a <= 10) {
   //do some stuff
}

in place of your do-while loops

You have to keep a counter and initialize it with 0 initially.

On your onClickListener increase this counter variable value by one and check whether it is is equal to 10. If it so then display stopped.

private int counter; // Instance variable

public void onClick(View v) {
    counter++;
    if(counter == 10) {
        // display the stop on your textview here
    }
    ...do whatever you want here
}

A button's onClickListener executes every time the button is clicked. You need to have a variable(s) at the class level that serves as a counter that will count the number of clicks. Each time the button is clicked, you increment this counter by one. Finally, when this counter reaches 10, you will set the TextView to display stopped.

Also, it might be a better option to use the 'if' clause rather than do-while.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top