Android development creation of clickable button implementation method _android

Source: Internet
Author: User

This article is an example of how Android creates a clickable button implementation. Share to everyone for your reference, specific as follows:

Feel the need to learn about the development of mobile phone knowledge, whether for future work needs or current company projects.

Of course, the beginning of any new things, inevitably accompanied by the first helloworld,android learning is no exception. Since the beginning, I will not do too much description.

For Android Development Ide:adt, open first glance a bit confused, but looked at the web directory structure of the introduction, slowly understand, do this example, we particularly need to focus on two places, one is the SRC directory, one is the res directory under the layout directory. The SRC directory is where the Code-behind source code is placed, and the layout directory is the XML foreground configuration file.

Now that we want to implement the function is to click on the button, and then EditText display "Hello world!". Let's first open the layout file, drag and drop a button up, and then drag and drop a edittext up, the final XML file structure is as follows:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" 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 "> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "Android:layout_h" eight= "Wrap_content" android:clickable= "true" android:text= "button"/> <edittext android:id= "@+id/edit" Text1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layout_alignbaseline=" @+id/button1 "android:layout_alignbottom=" @+id/button1 "android:layout_torightof=" @+id/button1 "android:text="

 EditText "/> </RelativeLayout>

Then in the background code file, we need to introduce two namespaces:

Import Android.widget.Button;
Import Android.widget.EditText;

All the code is as follows:

Package Com.example.helloworld;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
  public class Mainactivity extends activity {private Button MyButton;
  Private EditText MyText;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    MyButton = (Button) Findviewbyid (R.id.button1);
    MyText = (edittext) Findviewbyid (R.ID.EDITTEXT1); Mybutton.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {//To
      Do auto-generated method Stub Mytext.settext ("Hello world!");
  }
    });  @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar
    If it is present.
    Getmenuinflater (). Inflate (R.menu.main, menu);
  return true;

 }
}

Then click the Run button, click the button in the virtual machine interface, and get the result as follows:

This section is here, and we will continue to explore the following.

For more information on Android-related content readers can view the site topics: "Android Debugging techniques and common problems solution summary", "Android Development introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", " Android Basic Components Usage Summary, Android View tips Summary, Android layout layout tips and Android Control usage summary

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.