How do I assign multiple events to the same button?

1

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

How do I achieve 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);
}
...
    
asked by anonymous 27.12.2017 / 22:18

1 answer

0

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

    
27.12.2017 / 23:07