S2. Android commonly used controls

[ Overview ]

  • Button (push button)

Button

  • In MainActivity activity_main.xml corresponding layout file, a graphics editor to add a button, as shown below:

  

  • activity_main.xml Button will add the code manually modify the id, text information, add an onClick event
    <Button
        android:id="@+id/testBtn"
        android:layout_width="150dp"
        android:layout_height="43dp"
        android:onClick="onClick"
        android:text="测试按钮"
        tools:layout_editor_absoluteX="130dp"
        tools:layout_editor_absoluteY="46dp" />
  • Adding onClick method MainActivity.java file: Activity with a click event can use the same method to onClick response, distributed via switch / case.
    void the onClick protected (View V) { 
        Switch (v.getId ()) { 
            Case R.id.testBtn: 
                Toast.makeText (the this, "test button is clicked", Toast.LENGTH_SHORT) the .Show (); 
        } 
    }

Guess you like

Origin www.cnblogs.com/zlxyt/p/11125568.html