Just learning Android, tangled in an event-based processing at the callback.
See "Crazy Java handouts" above:
First a custom Mybutton, inherited the View Button component and then reference
in main.xml layout file
But the translation of a problem.
In the investigation of online information, directly above the Activity by:
MyButton btn = new MyButton (this);
setContentView (btn);
There is also a problem, tangled for a long time, seeking to rescue the god ah!
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Slowly tangled it, correct the correction with enough
Reply:
Reply:
What is the problem ah? ? ? Description
Reply:
Event processing is based on callback, the callback method override does not work, but do not know the custom of View components, how to use the Manifest.xml file.
Reply:
Where callback? View custom written on the package name + type can be referenced in the xml, but there must be parameters for the constructor Context and AttributeSet requires a new custom view, you need to have a constructor parameter Context
Reply:
Upstairs answered correctly.
Reply:
This is a custom MyButton
public class MyButton extends Button {
public MyButton (Context context) {
super (context);
/ / TODO Auto-generated constructor stub
}
public boolean onKeyDown (int keyCode, KeyEvent event) {
/ / TODO Auto-generated method stub
Log.i ("keydown", "Hand in MyButton!");
return super.onKeyDown (keyCode, event);
}
}
This is the main.xml file references it
android: layout_width = "fill_parent"
android: layout_height = "wrap_content"
android: text = "Button" />
Or wrong, it should be how to write it?
Reply:
Why engage in a long time android, Bangding
Reply:
Xml defined by time, must achieve three constructor; You just implemented a
Reply:
Ah, really, to three constructors, thank you.
Internet looking for a long time did not find the answer, or the forum to force ah!
Reply:
Great God, I ask which of three built functions to achieve?
Reply:
I also follow the madness handouts do, catlog there are no tips "-work.mybutton-", "the onKeyDown in MyButton"
clickedYou help me to see where there is no configuration or configuration errors
This is the xml layout file
android: id = "@ + id / container"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: orientation = "vertical"
tools: context = "work.mybutton.MainActivity"
tools: ignore = "MergeRootFrame"
>
android: layout_height = "wrap_content"
android: text = "! Click me"
android: id = "@ + id / mybutton"
/>
Button
new definition of re-public class MyButton extends Button {
public MyButton (Context context) {
super (context);
/ / TODO Auto-generated constructor stub
}
public MyButton (Context context, AttributeSet attrs) {
super (context, attrs);
/ / TODO Auto-generated constructor stub
}
public MyButton (Context context, AttributeSet attrs, int defStyle) {
super (context, attrs, defStyle);
/ / TODO Auto-generated constructor stub
}
@ Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
/ / TODO Auto-generated method stub
super.onKeyDown (keyCode, event);
Log.v ("-work.mybutton-", "the onKeyDown in MyButton");
return true;
}
}
Main
public class MainActivity extends Activity {
MyButton mybutton = null;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
mybutton = (MyButton) findViewById (R.id.mybutton);
}
}
No comments:
Post a Comment