แน่นอนมันสามารถทำได้
ฉันคิดว่าวิธีที่ง่ายที่สุดสำหรับสถานการณ์ของคุณ และคุณสามารถขยายเวลาได้อย่างง่ายดายคือการสร้างฟังก์ชันตัวช่วยบางอย่างที่ดูแล:
1) การสร้างหน้าจอว่าง 2) สร้างปุ่มสำหรับหน้าจอ 3) สร้างมุมมองข้อความสำหรับหน้าจอและสุดท้าย 4) สร้างหน้าจอและเติมข้อมูล
คุณต้องตัดสินใจเลือกองค์ประกอบรูทที่เหมาะสมสำหรับ Views ของคุณ ขึ้นอยู่กับการจัดเรียงลูกที่คุณต้องการ เพื่อความง่าย ให้เราเลือก 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: การขยายมุมมอง สร้าง .java ใหม่ชื่อ "MyCoolScreen.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.
อาจมีข้อผิดพลาด เพียงพิมพ์รหัสที่นี่ แต่ฉันหวังว่าจะช่วยคุณได้!
person
rupps
schedule
14.05.2013