So I´m having three Buttons which lead to other Activities with a normal Click. Now i want to name the buttons through a longer Click and User Input. So short click goes forward, long Click ability to rename it. So far i was able to rename the button with a long click, but only to a predetermined String. My code so far.
public class MainActivity<button1> extends AppCompatActivity { Button button; Button button1; Button button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button3); button.setOnClickListener(v -> opensecondone()); button.setOnLongClickListener(v -> naming()); button1 = findViewById(R.id.button55); button1.setOnClickListener(a -> opensecondsecond()); button1.setOnLongClickListener(v -> naming1()); button2 = findViewById(R.id.button40); button2.setOnClickListener(a -> opensecondthird()); button2.setOnLongClickListener(v -> naming2()); } public boolean naming() { button = this.findViewById(R.id.button3); button.setText("message"); return true; } public boolean naming1() { button = this.findViewById(R.id.button55); button.setText("Your Text"); return true; } public boolean naming2() { button = this.findViewById(R.id.button40); button.setText("Your Text"); return true; } public void opensecondone() { Intent intent = new Intent(this, firmaeins.class); startActivity(intent); } public void opensecondsecond() { Intent intent = new Intent(this, firmaZwei.class); startActivity(intent); } public void opensecondthird() { Intent intent = new Intent(this, firmaDrei.class); startActivity(intent); } }
Advertisement
Answer
Getting text user input can be accomplished by using the EditText class which can hold user input.
Once you have setup your EditText, you can use the user input in your naming function using getText().
- Define an EditText.
EditText editText1
- Find the reference for the EditText.
editText1 = (EditText)findViewById(R.id.edittext);
- The user input is now available from the EditText object.
public boolean naming() { button = this.findViewById(R.id.button3); button.setText(editText1.getText().toString()); return true; }