How to assign multiple events to the same button?

Asked

Viewed 67 times

1

I have a layout where I can only have one button and need to use the same layout for various events onClick in a cardView.

How do I get this?

    public void onClick(View arg0) {
        switch (arg0.getContext()) {
            case getClass().getMethod(onActivityResult(2,2,null),MainActivity.class): {
                String value = editText.getText().toString();
                Intent intent = new Intent();
                intent.putExtra("VALUE", value);
                setResult(2, intent);
                finish();
            }

            case onActivityResult(3, 3, null): {
                String value = editText.getText().toString();
                Intent intent = new Intent();
                intent.putExtra("VALUE", value);
                setResult(3, intent);
                finish();
            }

            case onActivityResult(4, 4, null) {
                String value = editText.getText().toString();
                Intent intent = new Intent();
                intent.putExtra("VALUE", value);
                setResult(4, intent);
                finish();
            }
        }
        ...



    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==2) {
        String value = data.getStringExtra("VALUE");
        SharedPreferences sharedpreferences = getSharedPreferences("pref", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString("str_textview", value);
        editor.apply();
        textView1.setText(value);

    } else if(requestCode==3) {
        String value = data.getStringExtra("VALUE");
        SharedPreferences sharedpreferences = getSharedPreferences("pref", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString("str_textview", value);
        editor.apply();
        textView2.setText(value);
    }
    ...


    public void onClickSaldo(View arg0) {
    Intent intent=new Intent(this,StarterActivity.class);
    startActivityForResult(intent, 2);
}

public void onClickDizimo(View arg0) {
    Intent intent=new Intent(this,MainActivity.CashActivity.class);
    startActivityForResult(intent, 3);
}

public void onClickMercado(View arg0) {
    Intent intent=new Intent(this,MainActivity.CashActivity.class);
    startActivityForResult(intent, 4);
}
...

1 answer

0

If you want to use more than one event on a single button recommend using the Toggle Button, I believe there must be a way to add more states to the Toggle Button

Browser other questions tagged

You are not signed in. Login or sign up in order to post.