The button control of Android programming with Toast control usage Analysis _android

Source: Internet
Author: User
Tags event listener

This example describes the button control for Android programming with the toast control usage. Share to everyone for your reference, specific as follows:

In this chapter tutorial, we will learn about the use of the button control and, incidentally, the toast prompt control.

In the development of Android, the most common user interaction controls we use may be button, and the most common event we use is the onclick event.

These events are also the simplest event, we generally through Google's own API interface can be called, we specifically see how to do it.

First step. Create a new project Ep.toast, active and main view names I use the default, set the views Activity_main.xml:

 <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 "> <textview android:id=" @+id/textview1 "android:layout_width=" Wrap_content "Android:layo" ut_height= "Wrap_content" android:text= "@string/hello_world"/> <button android:id= "@+id/button1" Andro Id:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentright= "true" and roid:layout_aligntop= "@+id/textview1" android:layout_marginright= "61DP" android:text= "OK"/> </RelativeLayou T> 

Inside is a text label and a button control.

Step two. We write the core file--mainactivity.java, here I will try to give you more comments:

 package com.example.ep.toast; import android.os.Bundle; import android.app.Activity; import
Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.TextView;
Import Android.widget.Toast;
  public class Mainactivity extends activity {private TextView txtview1;
  Private Button btn1;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    Bound text Label control txtview1= (TextView) Findviewbyid (R.ID.TEXTVIEW1);
    Bind button control btn1= (button) Findviewbyid (R.id.button1); Add a click event Listener and instantiate the listener Btn1.setonclicklistener for a click event (new Onclicklistener () {@Override public void OnClick (V
        Iew v) {//click button to set text label Txtview1.settext ("Joven");
      Click on the button after the pop-up prompt box, which parameters are (binding activity, prompt content, display time) Toast.maketext (Mainactivity.this, "Joven", Toast.length_long). Show ();
  }
    }); }
}

Finally, we can see the effect.

Okay, here's what this chapter is all about, though these are the basics that can't be built on. But no matter how big and complex the project is, it is actually built on these things. Knowledge is accumulated before it becomes the essence.

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

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.