GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — NB_Android/code_18-20_events.txt at master · glwhitney/NB_Android Expand file tree/ Copy pathcode_18-20_events.txtMore file actions 135 lines (112 loc) · 5.23 KB / Copy pathcode_18-20_events.txtFile metadata and controls135 lines (112 loc) · 5.23 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 [source code] Android Development Tutorials 18 thru 20 - Events ***** Tutorial Notes ** 18 - Event Handling- code available //Overview// All the Activity Lifecycle callback methods were generated by Android. Here we are going to learn how to listen for a click event on a button. We are also going to change the text in a TextView as a visual clue that we responded to the event. // Tutorial// 1. Start with Empty Activity. Delete Hello World. a. Add large text and a button. Give them both ids. Convert Strings to resource. NOTE: The ids Bucky chose both use the standard of ending the id with the object type i.e. buckysText, and buckysButton 2. In the java file 3. import android.view.View; import android.widget.Button; import android.widget.TextView; 4. Discussed Event Listener - listen for an event, and the Callback Method - what to do ** 19- Event Listener and Callback Method 1. Listener goes in onCreate() a. get reference b. setOnClickListener()is the interface (callback Method)goes inside Anonymous inner class 2. Get a reference to the text and use setText() NOTE: You don't have to use setOnclickListener if you use the attribute android:onClick( ) in the button element. But you still need a class that implements the Button.onClickListener interface. For example, if you have a bunch of buttons then you might want to define one inner class that implements the interface. ** 20 - Multiple Event Listeners 1. Multiple events on the same button. Start with the code from 19. 2. Long click is holding down more than second. 3. New listener set.onlongclick listener, in oncreate but outside of the other listener. a. Needs a return statement. Return true means the event was handled (consumend). Otherwise Andoid calls the click listener too. //Null warning// The click events could return null, so Studio highligts them in yellow. Just surround them with: If (buckysText !null) ***** manifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.thenewboston.smartbutton"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> ***** MainActivity.java package com.thenewboston.smartbutton; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button buckysButton = (Button)findViewById(R.id.buckysButton); buckysButton.setOnClickListener( new Button.OnClickListener(){ public void onClick(View v){ TextView buckysText = (TextView)findViewById(R.id.buckysText); buckysText.setText("Good job Hoss!"); } } ); // Tutorial 20 longClickListener buckysButton.setOnLongClickListener( new Button.OnLongClickListener(){ public boolean onLongClick(View v){ TextView buckysText = (TextView)findViewById(R.id.buckysText); buckysText.setText("Holy carp, that was a long one!"); return true; } } ); } } ***** activity_main.xml <?xml version="1.0" encoding="utf-8"?> <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="com.thenewboston.smartbutton.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/message_text" android:id="@+id/buckysText" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="98dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_text" android:id="@+id/buckysButton" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout> ***** strings.xml <resources> <string name="app_name">SmartButton</string> <string name="message_text">Hello</string> <string name="button_text">Click Me Hass</string> </resources> |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |