Tuesday, April 8, 2014

Android View controls by a string reflection


C # through reflection, reflecting the controls. If now there is a "EditText" string, reflected by the string EditText control? I do not know you can not? I am now doing specific function is to survive the dynamic interface, XML file on the SD card, XML has control properties such that information by acquiring XML, dynamic display interface information, my thoughts directly controls the configuration string, and then reflecting controls, but does not, no other ideas, such as the configuration number, then the internal handling.
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Here is different from the program reflection reflection .. just based on XML, as defined in dynamic generation VIEW controls. Androia support the view objects created using java program directly, so you just have to develop a set of rules, then according to your XML rules to create VIEW object to.
Reply:
I think that the landlord is not needed layoutinflater


Reply:
The reply was deleted administrator at 2013-07-10 10:35:23

Reply:
cited a floor a105865708 reply:
reflection here is different from the program .. just a reflection of the XML definition, dynamic generation VIEW controls . androia support the view objects created using java program directly, so you just have to develop a set of rules, then the rules according to your XML to create VIEW object to.


I have completed by this method, but each control is created when too many methods. Such as: When creating EditText EditText need to create a way to write, you need to create a method to create EditText TextView, ask the great God has not created child controls by View? ? Online. . .
Reply:
reference to the second floor guoyoulei520 reply:
I think, this is not the landlord is not required to read the internal resources with layoutinflater
, Instead use the XML file, not the XML layout, Ken can layoutinflater also less than, or that you have had a similar experience in development? Seeking advice. . .
Reply:
references, 4th Floor zz536088 reply:
Quote: references to a floor a105865708 reply:

Here is different from the program reflection reflection .. just based on XML, as defined in dynamic generation VIEW controls. Androia support the view objects created using java program directly, so you just have to develop a set of rules, then according to your XML rules to create VIEW object to.


I have completed by this method, but each control is created when too many methods. Such as: When creating EditText EditText need to create a way to write, you need to create a method to create EditText TextView, ask the great God has not created child controls by View? ? Online. . .


Written tools:

 public class XmlUtil {
public View XmlToView (String xml) {
/ / TODO
return view;
}
}


Reply:
reference to the 6th floor a105865708 reply:
Quote: references to the 4th floor zz536088 reply:

Quote: references to a floor a105865708 reply:

Here is different from the program reflection reflection .. just based on XML, as defined in dynamic generation VIEW controls. Androia support the view objects created using java program directly, so you just have to develop a set of rules, then according to your XML rules to create VIEW object to.


I have completed by this method, but each control is created when too many methods. Such as: When creating EditText EditText need to create a way to write, you need to create a method to create EditText TextView, ask the great God has not created child controls by View? ? Online. . .


Written tools:

 public class XmlUtil {
public View XmlToView (String xml) {
/ / TODO
return view;
}
}


Great God, can not be specific, I'm stuck in here, I'm not the kind of code directly to the people, to the links is required. Seeking advice ...
Reply:
reference to the 7th floor zz536088 reply:
Quote: references to the 6th floor a105865708 reply:

Quote: references to the 4th floor zz536088 reply:

Quote: references to a floor a105865708 reply:

Here is different from the program reflection reflection .. just based on XML, as defined in dynamic generation VIEW controls. Androia support the view objects created using java program directly, so you just have to develop a set of rules, then according to your XML rules to create VIEW object to.


I have completed by this method, but each control is created when too many methods. Such as: When creating EditText EditText need to create a way to write, you need to create a method to create EditText TextView, ask the great God has not created child controls by View? ? Online. . .


Written tools:

 public class XmlUtil {
public View XmlToView (String xml) {
/ / TODO
return view;
}
}


Great God, can not be specific, I'm stuck in here, I'm not the kind of code directly to the people, to the links is required. Seeking advice ...

I also thought about doing, but the card can not move forward.
Reply:
Great God not when
XML definitions for some of you posted, I see nothing thinking.
Reply:
reference to the 9th floor a105865708 reply:
Great God not when
XML definitions for some of you posted, I see nothing thinking.


private void createEditView (String _text, String _width, String _height, String _ID, String _ischageLine, String _parentID)
{
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams (
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);

if (Integer.parseInt (_ischageLine)> 0)
/ / Line flag, wrap, wrap if greater than 0
lp.addRule (RelativeLayout.BELOW, Integer.parseInt (_parentID));
lp.addRule (RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

EditText editTxt = new EditText (this);
editTxt.setWidth (Integer.parseInt (_width));
editTxt.setHeight (Integer.parseInt (_height));
editTxt.setId (Integer.parseInt (_ID));
rl.addView (editTxt, lp);
}

/ **
* Create a Button control
* @ Param _text
* @ Param _width
* @ Param _height
* @ Param _ID
* @ Param _ischageLine
* @ Param _parentID
* /
private void createButton (String _text, String _width, String _height, String _ID, String _ischageLine, String _parentID)
{
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams (
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);

if (Integer.parseInt (_ischageLine)> 0)
/ / Line flag, wrap, wrap if greater than 0
lp.addRule (RelativeLayout.BELOW, Integer.parseInt (_parentID));
lp.addRule (RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

Button btn = new Button (this);
btn.setWidth (Integer.parseInt (_width));
btn.setHeight (Integer.parseInt (_height));
btn.setText (_text);
btn.setId (Integer.parseInt (_ID));
btn.setOnClickListener (new MyOnClickListener ()) ;/ / register the event
rl.addView (btn, lp);
}

This is the main feature, the problem is that these two combined to create a method like, wondered not, because even create many different controls. While gamblers XML nodes by these two methods.
Reply:
reference to the 9th floor a105865708 reply:
Great God not when
XML definitions for some of you posted, I see nothing thinking.


No XML layout file is created by the control code.

<-! Type / control name (must have a certain normative, non-empty), id / ID number for each control (non-empty), text / control displays text
width / width dp, height / high dp, ischageLine / need to wrap, chageParentID / at the bottom of an element ID ->










This thing externally defined XML, created by this control.
Reply:
I said my idea:

First, define a class, which contains some of the attributes defined in xml, such as control type, control the size of the child controls so

Then traverse the XML, the XML becomes physical objects

Then based on the entity object, generate VIEW object (this VIEW objects may already contain child's VIEW)

Then put this view into the activity to go. .

This method can be written as a series of public methods. .
Reply:
reference to the 12th floor a105865708 reply:
I said my idea:

First, define a class, which contains some of the attributes defined in xml, such as control type, control the size of the child controls so

Then traverse the XML, the XML becomes physical objects

Then based on the entity object, generate VIEW object (this VIEW objects may already contain child's VIEW)

Then put this view into the activity to go. .

This method can be written as a series of public methods. .


According to what you mean, I wrote him a thin tools, and the definition of those attributes into physical objects, which I can understand, "This VIEW objects may already contain the child's VIEW" this do not understand? ?
Reply:
reference to the 12th floor a105865708 reply:
I said my idea:

First, define a class, which contains some of the attributes defined in xml, such as control type, control the size of the child controls so

Then traverse the XML, the XML becomes physical objects

Then based on the entity object, generate VIEW object (this VIEW objects may already contain child's VIEW)

Then put this view into the activity to go. .

This method can be written as a series of public methods. .


Results posted, and points gave you.
Reply:
 public class MainActivity extends Activity {

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);

String xml = "" +
" ; "+
"" +
" "+
"" +
" "+
"" +
" "+
"
";

Control control = new Control ();
control.setViewsFromXml (this, xml);

Collections / / has turned into the XML view of
List views = control.getViews ();

/ / Display the collection based on the need to interface
LinearLayout layout = (LinearLayout) findViewById (R.id.line);
System.out.println (views.size ());

for (View view: views) {
layout.addView (view);
}
}

}



 public class Control {
Context context;

private List views = new ArrayList ();

public List getViews () {
return views;
}

public void setViews (List views) {
this.views = views;
}

public void setViewsFromXml (Context context, String xml) {
this.context = context;
SAXParserFactory parserFactory = SAXParserFactory.newInstance ();
XmlHandler handler = new XmlHandler ();
try {
XMLReader xmlReader = parserFactory.newSAXParser () getXMLReader ();.
xmlReader.setContentHandler (handler);
xmlReader.parse (new InputSource (new StringReader (xml)));
} Catch (Exception e) {
e.printStackTrace ();
}
}

class XmlHandler extends DefaultHandler {
private View view;
private String text;
private int id, width, height, ischageLine, chageParentID;

@ Override
public void startDocument () throws SAXException {
super.startDocument ();
}

@ Override
public void startElement (String uri, String localName, String qName,
Attributes attributes) throws SAXException {
String type = attributes.getValue ("type");

if (type == null) {
return;
}

System.out.println ("type ->" + type);
if (type.equals ("TextView")) {
view = new TextView (context);
} Else if (type.equals ("EditText")) {
view = new EditText (context);
} Else if (type.equals ("Button")) {
view = new Button (context);
}

String widthStr = attributes.getValue ("width");
if (widthStr! = null) {
width = Integer.parseInt (widthStr);
}
System.out.println ("width ->" + widthStr);

String heightStr = attributes.getValue ("height");
if (heightStr! = null) {
height = Integer.parseInt (heightStr);
}
System.out.println ("height ->" + height);

text = attributes.getValue ("text");
System.out.println ("text ->" + text);

}

@ Override
public void endElement (String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase ("Item")) {
LayoutParams params = new LayoutParams (width, height);
view.setLayoutParams (params);
if (view instanceof TextView) {
TextView t = (TextView) view;
t.setText (text);
/ / ....
} Else if (view instanceof EditText) {
EditText t = (EditText) view;
t.setText (text);
} Else if (view instanceof Button) {
Button t = (Button) view;
t.setText (text);
}
views.add (view);
}
}

@ Override
public void characters (char [] ch, int start, int length)
throws SAXException {
}
}
}


 
xmlns: tools = "http://schemas.android.com/tools"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: paddingBottom = "@ dimen / activity_vertical_margin"
android: paddingLeft = "@ dimen / activity_horizontal_margin"
android: paddingRight = "@ dimen / activity_horizontal_margin"
android: paddingTop = "@ dimen / activity_vertical_margin"
tools: context = ". MainActivity"
android: id = "@ + id / line"
android: orientation = "vertical"
>


Reply:
cited 15 floor a105865708 reply:
 public class MainActivity extends Activity {

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);

String xml = "" +
" ; "+
"" +
" "+
"" +
" "+
"" +
" "+
"
";

Control control = new Control ();
control.setViewsFromXml (this, xml);

Collections / / has turned into the XML view of
List views = control.getViews ();

/ / Display the collection based on the need to interface
LinearLayout layout = (LinearLayout) findViewById (R.id.line);
System.out.println (views.size ());

for (View view: views) {
layout.addView (view);
}
}

}



 public class Control {
Context context;

private List views = new ArrayList ();

public List getViews () {
return views;
}

public void setViews (List views) {
this.views = views;
}

public void setViewsFromXml (Context context, String xml) {
this.context = context;
SAXParserFactory parserFactory = SAXParserFactory.newInstance ();
XmlHandler handler = new XmlHandler ();
try {
XMLReader xmlReader = parserFactory.newSAXParser () getXMLReader ();.
xmlReader.setContentHandler (handler);
xmlReader.parse (new InputSource (new StringReader (xml)));
} Catch (Exception e) {
e.printStackTrace ();
}
}

class XmlHandler extends DefaultHandler {
private View view;
private String text;
private int id, width, height, ischageLine, chageParentID;

@ Override
public void startDocument () throws SAXException {
super.startDocument ();
}

@ Override
public void startElement (String uri, String localName, String qName,
Attributes attributes) throws SAXException {
String type = attributes.getValue ("type");

if (type == null) {
return;
}

System.out.println ("type ->" + type);
if (type.equals ("TextView")) {
view = new TextView (context);
} Else if (type.equals ("EditText")) {
view = new EditText (context);
} Else if (type.equals ("Button")) {
view = new Button (context);
}

String widthStr = attributes.getValue ("width");
if (widthStr! = null) {
width = Integer.parseInt (widthStr);
}
System.out.println ("width ->" + widthStr);

String heightStr = attributes.getValue ("height");
if (heightStr! = null) {
height = Integer.parseInt (heightStr);
}
System.out.println ("height ->" + height);

text = attributes.getValue ("text");
System.out.println ("text ->" + text);

}

@ Override
public void endElement (String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase ("Item")) {
LayoutParams params = new LayoutParams (width, height);
view.setLayoutParams (params);
if (view instanceof TextView) {
TextView t = (TextView) view;
t.setText (text);
/ / ....
} Else if (view instanceof EditText) {
EditText t = (EditText) view;
t.setText (text);
} Else if (view instanceof Button) {
Button t = (Button) view;
t.setText (text);
}
views.add (view);
}
}

@ Override
public void characters (char [] ch, int start, int length)
throws SAXException {
}
}
}


 
xmlns: tools = "http://schemas.android.com/tools"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: paddingBottom = "@ dimen / activity_vertical_margin"
android: paddingLeft = "@ dimen / activity_horizontal_margin"
android: paddingRight = "@ dimen / activity_horizontal_margin"
android: paddingTop = "@ dimen / activity_vertical_margin"
tools: context = ". MainActivity"
android: id = "@ + id / line"
android: orientation = "vertical"
>



I try Ha, thank you, worship ah!
Reply:
reference to the 12th floor a105865708 reply:
I said my idea:

First, define a class, which contains some of the attributes defined in xml, such as control type, control the size of the child controls so

Then traverse the XML, the XML becomes physical objects

Then based on the entity object, generate VIEW object (this VIEW objects may already contain child's VIEW)

Then put this view into the activity to go. .

This method can be written as a series of public methods. .


Thank you, because just learning Android, I do not know you can do this, once again worship, knot posted. Haha
Reply:
Will the landlord, you control the reflection generate success yet?

No comments:

Post a Comment