Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I keep my Java Android code short?
    primarykey
    data
    text
    <p>Hi I am developing an Android app using an EditText, and a add en minus button to control it's value. I got it working now, but I wanna repeat it to multiple values. I know I can just repeat the code with different variables but the code would big really large.</p> <p>Anyone an idea how to repeat the same code for multiple values?</p> <p>This is my current code:</p> <pre><code>package com.lars.MyApp; import com.lars.MyApp.R; import android.app.Activity; import android.os.Bundle; import android.widget.Button; import android.widget.EditText; import android.view.View; import android.view.View.OnClickListener; public class MyAppActivity extends Activity { int currentValue = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final EditText Value = (EditText) findViewById(R.id.Value); Button addButton = (Button) findViewById(R.id.addButton); Button minusButton = (Button) findViewById(R.id.minusButton); addButton.setOnClickListener(new OnClickListener(){ public void onClick(View v) { addValue(); Value.setText("" + currentValue); } }); minusButton.setOnClickListener(new OnClickListener(){ public void onClick(View v) { minusValue(); Value.setText("" + currentValue); } }); } public void addValue(){ if(currentValue &lt;= 999){ currentValue = currentValue + 1; } } public void minusValue(){ if(currentValue &gt;= 1){ currentValue = currentValue - 1; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload