Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

For the last days I have been trying to implement a Edittext Dialog which the user introduces an 8-bit array (1 or 0) and then the application will transform this bit array into decimal and hexadecimal value (if it is 11111111 it will be dec=255 and hex=0xFF) but I don't know hot to implement this case. If it is not possible to implement in this way, it will be also posssible to instead of a bit input, it can be a int/decimal input.

Here is the code I have implemented to show the Edittext Dialog:

else if (uuid.equals(BleDefinedUUIDs.Characteristic.PASSWORD)){
                    final EditText passtext = new EditText(v.getContext());
                    new AlertDialog.Builder(v.getContext())
                    .setTitle("Password Modification")
                    .setMessage("Please, introduce the new 8-bit password (with 1 or 0):")
                    .setView(passtext)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            // Code to obtain the the bit array/int from the edittext box
                        }

                    })
                    .show();
                }

Does anyone know how to implement this?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Here is an example of how you could possibly implement this functionality. I've used your code and modified it slightly.

MainActivity.java

package com.example.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.method.DigitsKeyListener;
import android.util.Log;
import android.widget.EditText;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final EditText passtext = new EditText(this);

        // Set the length to 8 characters
        passtext.setFilters(new InputFilter[] { new InputFilter.LengthFilter(8) });

        // Set the keypad to numeric
        passtext.setInputType(InputType.TYPE_CLASS_NUMBER);

        // Only allow the user to enter 0 and 1 numbers
        passtext.setKeyListener(DigitsKeyListener.getInstance("01"));

        new AlertDialog.Builder(this).setTitle("Password Modification").setMessage("Please, introduce the new 8-bit password (with 1 or 0):").setView(passtext).setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                String binaryDigits = passtext.getText().toString().trim();

                if (binaryDigits != null && !binaryDigits.isEmpty()) {

                    // Convert the entered digits to decimal
                    int decimalValue = Integer.parseInt(binaryDigits, 2);

                    // Convert the entered digits to hex
                    String hex = Integer.toHexString(decimalValue);

                    Log.d(MainActivity.class.getSimpleName(), "Decimal: " + decimalValue);
                    Log.d(MainActivity.class.getSimpleName(), "Hex: " + hex);
                }
            }

        }).show();
    }
}
share|improve this answer
    
Thanks for the help!!! It works perfectly to introduce and save the value. I have another request. I would like to do the same but now I want to introduce a number with at least two decimals. It will be like introducing a time between 0.1s to 10s. You know how to this? Thanks again for the help –  user255357 2 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.