On-Screen Buttons for android apps?
How do I create on-screen buttons for android apps. I am using the SDK's Lunar Lander game as a base, and I want to make it so that you don't need a keyboard开发者_如何学Go.
To create a button you add something like this in your layout XML file:
<Button
android:id="@+id/button_1"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:text="Button 1"/>
To get a hook to the button in your code where you can add some action when the button is pressed add an OnClickListener:
findViewById(R.id.button_1).setOnClickListener(new MyButtonListener());
Declare the OnClickListener as a private class (or inline)
private class MyButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
// Your code doing something cool goes here...
System.out.println("Click!");
}
}
Please look in the documentation that Mr Dittmar posted links to for more details, but this should hopefully get you started. :)
Take a look at the documentation on how to create a layout. After that, the documentation on how to create and handle buttons might come handy.
精彩评论