我有兴趣使用用户定义的相同视图数填充屏幕/活动。每个视图将具有完全相同的布局:耦合TextViews和几个按钮。问题是每个按钮将控制每个TextView将显示的内容。
我想要实现它的方式是拥有一个XML和一个Java类。然后依赖用户输入的数字,用许多相同的视图填充屏幕(使用for循环)。问题是,可以做到吗?怎么样?我是否以正确的方式思考它?
请帮助任何输入或想法,代码示例也会很棒。
答案 0 :(得分:1)
当然可以做到。
我认为最简单的情况,加上你可以轻松扩展,就是创建一些帮助函数来处理:
1)创建一个空屏幕 2)为屏幕创建一个按钮 3)为屏幕创建文本视图 最后 4)创建一个屏幕并填充它
您必须根据所需的子项规则确定视图的正确Root元素。为简单起见,我们选择一个LinearLayout,但对于RelativeLayout或TableLayout,示例是相同的,它只会在添加元素时更改,您必须使用其他参数来正确放置它们。
请注意,创建空自定义视图的函数返回ViewGroup(“所有布局派生自的地方”)。这样,您始终可以使用ViewGroups,只需在createCustomView中定义一次屏幕布局类型。所以你可以改变那里的屏幕类型,剩下的代码就可以了......
以下是您的灵感代码:
private ViewGroup createCustomView(Context context) {
LinearLayout myCoolNewView=new LinearLayout(context); // or RelativeLayout, etc..
return myCoolNewView;
}
private Button createButton(Context context, String buttonText) {
Button newButton=new Button(context);
newButton.setText(buttonText);
return newButton;
}
private TextView createText(Context context, String initialText) {
TextView newText=new TextView(context);
newText.setText(buttonText);
return newText;
}
private ViewGroup createScreen(Context context, int numberOfButtons, int numberOfTextfields) {
ViewGroup newScreen=createCustomView(context);
TextView[] textViews=new TextView[numberOfTextFields];
for (int i=0; i<numberOfTextfields; i++) {
textViews[i]=createText(context, "hi i am text "+i);
newScreen.addView(textViews[i]); // you ideally provide here layoutparams to properly place your buttons
}
for (int j=0; i<numberOfButtons; j++) {
Button button=createButton(context, "hi i am button "+j);
button.setOnClickListener(new OnClickListener() {
public void onClick (View clickedView) {
// here you have a button keypress and you know all the textviews
textView[i%j].setText("hey you pressed me");
}
});
newScreen.addView(button);
}
return newScreen;
}
现在你可以:
ViewGroup screen1=createScreen(context, 10, 10);
ViewGroup screen2=createScreen(context, 5, 3);
ViewGroup screen3=createScreen(context, 2, 5);
并将屏幕添加到父布局,ViewFlipper,ViewSwitcher等......像这样:
ViewGroup parentLayoutOfAllScreens=findViewById(R.id.root_of_screens);
parentLayoutOfAllScreens.addView(screen1);
parentLayoutOfAllScreens.addView(screen2);
parentLayoutOfAllScreens.addView(screen3);
在XML中,您只需创建根布局,并将其命名为root_of_screens ...
好的编码!!!我想上面的代码中会有一些错误,只需在这里输入,但我希望你能得到这个想法并根据你的需要进行调整!编辑:v2.0:扩展视图 在活动所在的同一文件夹中创建一个名为“MyCoolScreen.java”的新.java或任何名称:(简单):
package ........
public class MyCoolScreen extends LinearLayout {
/** Now every view holds its own buttons, and they are private, it's good for encapsulating */
private TextView[] mTextViews; // <-- as a convention, members should start with "m"
private Button[] mButtons;
private UserPressedButtons mUserPressedButtonsListener; // See below
/** The following constructors must always be present for a custom view, and must always call super */
public MyCoolScreen(Context context) {
// This is the constructor you will use when creating your view programmatically
super(context);
}
public MyCoolScreen(Context context, AttributeSet attrs) {
// This is the constructor Android calls when you include your custom view in an XML
// You can do this too!!
// The ATTRS will then include your numberofbuttons and numberoftextfields from the XML
// this is beyond the example, but read about it, it's interesting
super(context, attrs); // this MUST ALWAYS be here for custom views, or they will not work.
// it tells the parent view to continue the construction.
}
public MyCoolScreen(Context context, AttributeSet attrs, int defStyle) {
// Another constructor Android calls from the XML
super(context, attrs, defStyle);
}
/** We create an "init" method to initialize this view from outside */
public void init(int numberOfTextViews, int numberOfButtons) {
createScreen(numberOfTextViews, numberOfButtons);
}
/** This is the same */
private Button createButton(Context context, String buttonText) {
Button newButton=new Button(context);
newButton.setText(buttonText);
return newButton;
}
/** This is the same */
private TextView createText(Context context, String initialText) {
TextView newText=new TextView(context);
newText.setText(buttonText);
return newText;
}
/** We tweak this function so it doesnt return a view, but rather fills up this one :) */
private void createScreen(int numberOfButtons, int numberOfTextfields) {
ViewGroup newScreen=this; // It's this view the one we gonna fill up!
mTextViews=new TextView[numberOfTextfields];
mButtons=new Button[numberOfButtons];
Context context=getContext(); // Views always know their context after constructed
for (int i=0; i<numberOfTextfields; i++) {
mTextViews[i]=createText(context, "hi i am text "+i);
newScreen.addView(textViews[i]); // you ideally provide here layoutparams to properly place your buttons
}
for (int j=0; i<numberOfButtons; j++) {
Button button=createButton(context, "hi i am button "+j);
button.setId(j);
button.setOnClickListener(new OnClickListener() {
public void onClick (View clickedView) {
// here you have a button keypress and you know all the textviews
if (mUserPressedButtonsListener!=null) mUserPressedButtonsListener.OnButtonPressed(j);
textView[i%j].setText("hey you pressed me");
}
});
mButtons[j]=button;
newScreen.addView(button);
}
}
public interface UserPressedButtons {
public void OnButtonPressed(int buttonNumber);
}
public void setUserPressedButtonsListener (UserPressedButtons listener) {
mUserPressedButtonsListener=listener;
}
}
好的,现在使用它,在你的活动中你可以做到:
import ....... .MyCoolScreen;
import ....... .MyCoolScreen.UserPressedButtons;
.
.
.
MyCoolScreen screen1=new MyCoolScreen(context);
screen1.init(5,5); // initializes the screen.
myRootLayout.addView(screen1);
这有什么好处,现在功能完全封装在您的自定义视图中。它驻留在另一个.java中,因此您的活动代码非常干净,您甚至可以扩展View功能而不会让它变得难看。
为您的视图创建接口和侦听器以与外界进行通信也是一种常见做法,例如,我们可以这样做:
screen1.setUserPressedButtonsListener(new MyCoolScreen.UserPressedButtons() {
@Override
public void OnButtonPressed (int number) {
// you know the user pressed button "number", and you can do stuff about it without
// having to include it inside the MyCoolScreen class. Of course in your example you
// don't need this at the moment, because the View will modify its textfield, but suppose
// one of the buttons is "rocket launch" , that is something you will handle at the activity level, ie.
if (number==ROCKET_LAUNCH) RocketLauncher.setTarget(10,10).launch(); // Your MyCoolScreen doesnt know how to launch rockets, but your activity maybe yes...
}
});
您可以使用新的自定义视图执行各种酷炫的操作。例如,您可以定义:
@Override
public void OnDraw(Canvas c) {
c.drawEllipse ...
c.drawRectangle ....
}
你可以在你的文本区域上绘制圆圈,线条等等。按钮:)为此,你必须把
setWillNotDraw(false) on the constructor.
可能有错误,只需输入代码,但我希望它可以帮到你!
答案 1 :(得分:0)
Add and Remove Views in Android Dynamically?
这对你最有帮助......