Control personalizado de Android, ¿es esto posible?

Im searching this since yesterday. I've got on many Activities some piece of code which displays user name and login. I dont want to copy and paste code in layout into every Activity, but I want something just like user controls in .NET. I've read a lot of topic about custom controls but or I don't understand it or its not possible do to this (I dont belive in that option)

preguntado el 30 de junio de 12 a las 13:06

clearly say what you want!!! because answers may be dont know user controls in .NET. -

1 Respuestas

Yes it is possible. Look at this sample. It is a custom Numeric Keyboard or a custom View or a custom user control. Customize it and create your own user control:

import java.util.ArrayList;

import org.mabna.order.R;

import android.app.Activity;
import android.content.Context;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

// this NumericKeyboard works only for EditTexts which 
// have a tag with "usesNumericKeyboard" key and value of "true"
// use tag with "ignoreMeForNumericKeyboardTouchListener" key and 
// value "true" for controls you do not want to set its touchListener 
public class NumericKeyboard extends LinearLayout implements OnTouchListener {

    private View mainView = null;
    private EditText currentEditText;
    private Button btn0;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private Button btn5;
    private Button btn6;
    private Button btn7;
    private Button btn8;
    private Button btn9;
    private ImageButton btnBackSpace;
    private ImageButton btnDeleteAll;

    public NumericKeyboard(Context context) {
        super(context);
    }

    public NumericKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protected void initialize() {
        LayoutInflater layoutInflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater
                .inflate(R.layout.view_numeric_keyboard, this);

        btn0 = (Button) view.findViewById(R.id.btn0);
        btn1 = (Button) view.findViewById(R.id.btn1);
        btn2 = (Button) view.findViewById(R.id.btn2);
        btn3 = (Button) view.findViewById(R.id.btn3);
        btn4 = (Button) view.findViewById(R.id.btn4);
        btn5 = (Button) view.findViewById(R.id.btn5);
        btn6 = (Button) view.findViewById(R.id.btn6);
        btn7 = (Button) view.findViewById(R.id.btn7);
        btn8 = (Button) view.findViewById(R.id.btn8);
        btn9 = (Button) view.findViewById(R.id.btn9);
        btnBackSpace = (ImageButton) view.findViewById(R.id.btnBackSpace);
        btnDeleteAll = (ImageButton) view.findViewById(R.id.btnDeleteAll);

        btn0.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "0");
            }
        });
        btn1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "1");
            }
        });
        btn2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "2");
            }
        });
        btn3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "3");
            }
        });
        btn4.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "4");
            }
        });
        btn5.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "5");
            }
        });
        btn6.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "6");
            }
        });
        btn7.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "7");
            }
        });
        btn8.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "8");
            }
        });
        btn9.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "9");
            }
        });
        btnBackSpace.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "<");
            }
        });
        btnDeleteAll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editCurrentText(v, "D");
            }
        });

        if (mainView == null && !this.isInEditMode()) {
            setTouchListenerForChildViews();
            this.setVisibility(GONE);
        }
    }

    protected void editCurrentText(View v, String character) {
        if (currentEditText != null) {
            if (character.compareTo("<") == 0) {
                String text = currentEditText.getText().toString();
                if (text.length() == 0) {
                } else if (text.length() == 1) {
                    currentEditText.setText("");
                } else {
                    text = text.substring(0, text.length() - 1);
                    currentEditText.setText(text);
                }
            } else if (character.compareTo("D") == 0) {
                currentEditText.setText("");
            } else {
                String text = currentEditText.getText().toString();
                text += character;
                currentEditText.setText(text);
            }
        }
    }

    // @Override
    // protected void onLayout(boolean changed, int l, int t, int r, int b) {
    //
    // super.onLayout(changed, l, t, r, b);
    // }

    public void setTouchListenerForChildViews() {
        final Activity act = (Activity) getContext();
        mainView = act.getWindow().getDecorView()
                .findViewById(android.R.id.content);

        if (mainView == null)
            return;

        ArrayList<View> queue = new ArrayList<View>();
        queue.add((View) mainView);

        while (!queue.isEmpty()) {
            View v = queue.remove(0);
            if (v instanceof EditText && v.getTag(R.id.usesNumericKeyboard) == Boolean
                    .valueOf(true)) {
                ((EditText) v).setInputType(InputType.TYPE_NULL);
                ((EditText) v).setCursorVisible(true);
                v.setOnFocusChangeListener(new OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus)
                        {
                            for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
                                numericKeyboard.currentEditText = (EditText) v;
                                numericKeyboard.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                });
            }

            if (v instanceof NumericKeyboard) {
            } else {
                if (v.getTag(R.id.ignoreMeForNumericKeyboardTouchListener) != Boolean
                        .valueOf(true)) {
                    v.setOnTouchListener(this);
                }
                if (v instanceof ViewGroup) {
                    ViewGroup vg = (ViewGroup) v;
                    for (int i = 0; i < vg.getChildCount(); i++) {
                        View vChild = vg.getChildAt(i);
                        queue.add(vChild);
                    }
                }
            }
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (v instanceof EditText && v.getTag(R.id.usesNumericKeyboard) == Boolean
                .valueOf(true)) {
            for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
                numericKeyboard.currentEditText = (EditText) v;
                numericKeyboard.setVisibility(View.VISIBLE);
            }
        } else {
            for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
                numericKeyboard.setVisibility(View.GONE);
            }
        }
        return false;
    }

    static ArrayList<NumericKeyboard> arrNumericKeyboard =
            new ArrayList<NumericKeyboard>();

    public static void registerNumericKeyboard(
            NumericKeyboard numericKeyboard) {

        numericKeyboard.initialize();

        arrNumericKeyboard.add(numericKeyboard);
    }

    public static void unregisterNumericKeyboard(
            NumericKeyboard numericKeyboard) {
        arrNumericKeyboard.remove(numericKeyboard);
    }

    public static void showForEditText(EditText editText)
    {
        for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
            numericKeyboard.setVisibility(View.VISIBLE);
            numericKeyboard.currentEditText = editText;
        }
    }

    public static void hide()
    {
        for (NumericKeyboard numericKeyboard : arrNumericKeyboard) {
            numericKeyboard.setVisibility(View.GONE);
        }
    }
}

its layout as view_numeric_keyboard.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background06"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btn7"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:text="7" >
        </Button>

        <Button
            android:id="@+id/btn8"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:text="8" >
        </Button>

        <Button
            android:id="@+id/btn9"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:text="9" >
        </Button>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btn4"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:text="4" >
        </Button>

        <Button
            android:id="@+id/btn5"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:text="5" >
        </Button>

        <Button
            android:id="@+id/btn6"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:text="6" >
        </Button>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn1"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:text="1" >
        </Button>

        <Button
            android:id="@+id/btn2"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:text="2" >
        </Button>

        <Button
            android:id="@+id/btn3"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:text="3" >
        </Button>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/btnBackSpace"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:scaleType="fitCenter"
            android:src="@drawable/backspace"
            android:text="-" />

        <Button
            android:id="@+id/btn0"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:text="0" />

        <ImageButton
            android:id="@+id/btnDeleteAll"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:scaleType="fitCenter"
            android:src="@drawable/remove02"
            android:text="-" />

    </LinearLayout>

</LinearLayout>

using it in your Activity layout:

<org.mabna.order.ui.NumericKeyboard
                        android:id="@+id/numericKeyboard1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" >
                    </org.mabna.order.ui.NumericKeyboard>

Respondido 01 Jul 12, 04:07

I would add reference to this Romain Guy post Curious-creature.org/2009/02/25/… (because question was just about LAYOUT, but not any logic related to it). - estrella de arena

Unbelievable that you got only one vote for such an answer. It's typical for the Android community on stackoverflow, it's different in ( some of? ) the other groups. - nilo de roock

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.