How can I link XML pages with layout, by clicking on buttons?
Excuse the simplicity of this request - but is there a 开发者_StackOverflow社区way for me to link onClick commands for buttons through the Layout rather than code. I am trying to create a simple app and I want to be able to make buttons and have clicks go from one "page" to the other.
If not, could someone please point me in the direction of the code needed for it?
Is there a site that might overview how to use the UI to code for droid?
Thanks!
So just to make sure I get what your asking, when the user clicks on a button, say, 'Juice', they will be brought to a page that displays Juice?
First make a new class, in my example, a Juice.
protected void onCreate(Bundle savedValues) {
// Capture our button from layout
Button button = (Button)findViewById(R.id.mButton);
button.setOnClickListener(mJuiceClick);
}
private OnClickListener mJuiceClick = new OnClickListener() {
public void onClick(View v) {
//create a new intent that will launch the new 'page'
Intent i = new Intent(Main.this, Juice.class);
startActivity(i);
}
};
In most cases, you will make an xml layout for main, and one for juice, unless they display info in the same way. Go here for more info: http://developer.android.com/guide/topics/ui/ui-events.html
Try using
< Button android:onClick="myClickHandler" />
myClickHandler is a method that must be defined in your activity liek so:
public void myClickHandler(View target) {
// Do stuff
}
精彩评论