@author eliangela
*/
public class TecladoNumerico {
private LinearLayout principalLayout;
private StringBuilder texto = new StringBuilder();
private EditText ondeEscrever;
private List<EditText> ondeEscreverList = new ArrayList<EditText>();
private int focoAtual = 0;
//
private Context context;
private LinearLayout tecladoLayout;
private LinearLayout numerosLayout;
private float textSize = 30;
//layout dos numeros
private LinearLayout row1; //1,2,3
private LinearLayout row2; //4,5,6
private LinearLayout row3; //7,8,9
//
private LinearLayout.LayoutParams rowNumLayoutParams;
private LinearLayout.LayoutParams funcLayoutParams;
public TecladoNumerico(Context context) {
this.principalLayout = new LinearLayout(context);
this.context = context;
init();
buildNumerico();
}
private void init() {
tecladoLayout = new LinearLayout(context);
numerosLayout = new LinearLayout(context);
principalLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
funcLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
funcLayoutParams.gravity = Gravity.CENTER;
funcLayoutParams.weight = 0.5F;
}
private void buildNumerico() {
principalLayout.setOrientation(LinearLayout.VERTICAL);
numerosLayout.setOrientation(LinearLayout.VERTICAL);
tecladoLayout.setOrientation(LinearLayout.HORIZONTAL);
rowNumLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
rowNumLayoutParams.gravity = Gravity.CENTER;
rowNumLayoutParams.weight = 0.5F;
row1 = new LinearLayout(context);
row1.setOrientation(LinearLayout.HORIZONTAL);
Button n1Button = new Button(context);
Button n2Button = new Button(context);
Button n3Button = new Button(context);
Button n4Button = new Button(context);
Button n5Button = new Button(context);
Button n6Button = new Button(context);
Button n7Button = new Button(context);
Button n8Button = new Button(context);
Button n9Button = new Button(context);
n1Button.setTextSize(textSize);
n1Button.setText("1");
n2Button.setTextSize(textSize);
n2Button.setText("2");
n3Button.setTextSize(textSize);
n3Button.setText("3");
n4Button.setTextSize(textSize);
n4Button.setText("4");
n5Button.setTextSize(textSize);
n5Button.setText("5");
n6Button.setTextSize(textSize);
n6Button.setText("6");
n7Button.setTextSize(textSize);
n7Button.setText("7");
n8Button.setTextSize(textSize);
n8Button.setText("8");
n9Button.setTextSize(textSize);
n9Button.setText("9");
row1.addView(n1Button, rowNumLayoutParams);
row1.addView(n2Button, rowNumLayoutParams);
row1.addView(n3Button, rowNumLayoutParams);
row2 = new LinearLayout(context);
row2.setOrientation(LinearLayout.HORIZONTAL);
row2.addView(n4Button, rowNumLayoutParams);
row2.addView(n5Button, rowNumLayoutParams);
row2.addView(n6Button, rowNumLayoutParams);
row3 = new LinearLayout(context);
row3.setOrientation(LinearLayout.HORIZONTAL);
row3.addView(n7Button, rowNumLayoutParams);
row3.addView(n8Button, rowNumLayoutParams);
row3.addView(n9Button, rowNumLayoutParams);
numerosLayout.addView(row1);
numerosLayout.addView(row2);
numerosLayout.addView(row3);
LinearLayout.LayoutParams numLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
numLayoutParams.gravity = Gravity.CENTER;
numLayoutParams.weight = 0.3F;
tecladoLayout.addView(numerosLayout, numLayoutParams);
principalLayout.addView(tecladoLayout);
}
public void setTextSize(float size) {
this.textSize = size;
}
private void setOndeEscrever(EditText ondeEscrever) {
this.ondeEscrever = ondeEscrever;
if (ondeEscrever == null) {
this.texto = new StringBuilder();
return;
}
this.texto = new StringBuilder(this.ondeEscrever.getText());
this.ondeEscrever.requestFocus();
}
protected boolean isEmpty() {
return texto.length() == 0;
}
protected void insertString(String texto) {
if (ondeEscrever == null) {
return;
}
String txt = ondeEscrever.getText().toString();
if (!this.texto.toString().equals(txt)) {
this.texto = new StringBuilder(txt);
}
this.texto.append(texto);
ondeEscrever.setText(this.texto.toString());
}
public synchronized void backspace() {
if (ondeEscrever == null) {
return;
}
String txt = ondeEscrever.getText().toString();
if (!texto.toString().equals(txt)) {
texto = new StringBuilder(txt);
}
if (isEmpty()) {
return;
}
texto.deleteCharAt(texto.length() - 1);
ondeEscrever.setText(texto.toString());
}
public boolean isLastFocus() {
return focoAtual == ondeEscreverList.size() - 1;
}
public boolean isFirstFocus() {
return focoAtual == 0;
}
public void nextFocus() {
if (++focoAtual >= ondeEscreverList.size()) {
focoAtual = 0;
}
setOndeEscrever(this.ondeEscreverList.get(focoAtual));
}
public void prevFocus() {
if (–focoAtual < 0) {
focoAtual = this.ondeEscreverList.size() - 1;
}
setOndeEscrever(this.ondeEscreverList.get(focoAtual));
}
public void addOndeEscrever(EditText… edits) {
for (EditText v : edits) {
if (this.ondeEscreverList.contains(v)) {
throw new IllegalArgumentException("O mesmo EditText não pode ser adicionado mais de uma vez");
}
v.setInputType(0);
v.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View view, boolean isFocado) {
if (isFocado) {
setOndeEscrever((EditText) view);
focoAtual = ondeEscreverList.indexOf(view);
} else {
setOndeEscrever(null);
}
}
});
this.ondeEscreverList.add(v);
}
}
public void setVisivel(boolean visivel) {
principalLayout.setVisibility(visivel ? View.VISIBLE : View.INVISIBLE);
}
public boolean isVisivel() {
return principalLayout.getVisibility() == View.VISIBLE;
}
public LinearLayout getTecladoAsLinearLayout() {
return principalLayout;
}
}