我从昨天开始就在搜索这个。我在许多活动中都有一些显示用户名和登录名的代码。我不想将布局中的代码复制并粘贴到每个 Activity 中,但我想要一些类似于 .NET 中的用户控件的东西。我已经阅读了很多关于自定义控件的主题,但是或者我不理解它,或者它不可能这样做(我不相信那个选项)
问问题
1435 次
1 回答
10
是的,有可能。看看这个样本。它是自定义数字键盘或自定义视图或自定义用户控件。自定义它并创建您自己的用户控件:
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);
}
}
}
其布局为 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>
在您的活动布局中使用它:
<org.mabna.order.ui.NumericKeyboard
android:id="@+id/numericKeyboard1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</org.mabna.order.ui.NumericKeyboard>
于 2012-06-30T12:09:33.523 回答