Help me in a logic to solve my problem

Asked

Viewed 90 times

-1

Well, I’m creating an application for base conversions and I kind of created a mini keyboard with the keys to Hexadecimal (A, B, C, D, E, F), but it’s a hexadecimal text field when I type 3 and I’ll type through the mini-keyboard it does, it deletes the number typed and puts a letter. Anyway, I need to use a password to solve my problem, when I use my "mini keyboard" do not delete the number typed. The code is below.

`

private EditText Text_BIN;
private EditText Text_DEC;
private EditText Text_HEX;
private EditText Text_OCT;

private Button Button_BIN;
private Button Button_DEC;
private Button Button_HEX;
private Button Button_OCT;
private Button Button_CLEAN;
private Button A;
private Button B;
private Button C;
private Button D;
private Button E;
private Button F;
Boolean bool = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Text_BIN = (EditText) findViewById(R.id.text_BIN);
    Text_DEC = (EditText) findViewById(R.id.text_DEC);
    Text_HEX = (EditText) findViewById(R.id.text_HEX);
    Text_OCT = (EditText) findViewById(R.id.text_OCT);
    Button_BIN = (Button) findViewById(R.id.Button_BIN);
    Button_DEC = (Button) findViewById(R.id.Button_DEC);
    Button_HEX = (Button) findViewById(R.id.Button_OCT);
    Button_CLEAN = (Button) findViewById(R.id.Button_CLEAN);
    A = (Button) findViewById(R.id.A);
    B = (Button) findViewById(R.id.B);
    C = (Button) findViewById(R.id.C);
    D = (Button) findViewById(R.id.D);
    E = (Button) findViewById(R.id.E);
    F = (Button) findViewById(R.id.F);
}

public void Button_Clean(View v){
    Text_BIN.setText("");
    Text_DEC.setText("");
    Text_HEX.setText("");
    Text_OCT.setText("");
}

public void Button_BIN(View v){


}


public void A(View v){
    Text_HEX.setText("A"); //eu sei que assim está errado.
}

`

1 answer

-1


There are probably better solutions than this, but this one addresses the problem.

create a method to facilitate:

private void setText(EditText editText, String value) {
    //recupera a string do edittext
    String edit = editText.getText().toString();
    //Se ela não for nula
    if (!edit.isEmpty()) {
        //envia o valor do Botão ao final do texto
        editText.setText((edit + value));
        //retorna a seleção do edittext pra última posição
        editText.setSelection(edit.length() + 1);
    } else {
        //Se o edittext for vazio então envia o valor do botão nele
        editText.setText(value);
        //E novamente envia a seleção pra última posição
        editText.setSelection(1);
    }
}

Within each Click button event add this method informing which editText and what String value.

that is to say:

public void A(View v){
    setText(Text_HEX, "A");
}

I hope this helps you. Hug

Browser other questions tagged

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