70 likes | 149 Views
User Interface Layout Interaction. Interacting with a user. Events. Event Handlers/Listeners. The root of GUI elements is the View class. UI elements such as Buttons Labels Editable text Images Etc derive from the View class.
E N D
User Interface • Layout • Interaction
Interacting with a user. Events Event Handlers/Listeners
The root of GUI elements is the Viewclass. UI elements such as • Buttons • Labels • Editable text • Images • Etc • derive from the View class. • We define our app’s response to a click by implementing an instance of View.OnClickListenerinterface. Handling Clicks • public class View{ • … • //nested class • public interface OnClickListener { • void onClick(View v); • } • … • }
Click Handler idioms: 1. Have the class that owns the GUI element implement the OnClickListenerinterface. 2. Create an anonymous class that implements OnClickListenerinterface. 3. Create an anonymous class that implements OnClickListenerinterface.
1. Have the class that owns the GUI element implement the OnClickListenerinterface. • public class PixFragment extends Fragment implements View.OnClickListener{ • @Override • public View onCreateView(LayoutInflaterinflater, ViewGroupcontainer,BundlesavedInstanceState) { • View v = inflater.inflate(R.layout.pix_frag_layout, container, false); • ImageViewmSplashView = (ImageView)v.findViewById(R.id.pix_view); • mSplashView.setOnClickListener(this); • return v; • } • public void onClick(View v){ • // handle the click • Log.d("PixFragment", "I was clicked"); • } • }
2 Create an anonymous class that implements OnClickListenerinterface. • public class PixFragment extends Fragment{ • private View.OnClickListenermSplashViewListener = new View.OnClickListener() { • @Override • public void onClick(View v) { • // handle the click • Log.d("PixFragment", "I was clicked"); • } • }; • @Override • public View onCreateView(LayoutInflaterinflater, ViewGroupcontainer,BundlesavedInstanceState) { • View v = inflater.inflate(R.layout.pix_frag_layout, container, false); • ImageViewmSplashView = (ImageView)v.findViewById(R.id.pix_view); • mSplashView.setOnClickListener(mSplashViewListener); • return v; • } • }
3 Create an anonymous class that implements OnClickListenerinterface. • public class PixFragment extends Fragment implements View.OnClickListener{ • @Override • public View onCreateView(LayoutInflaterinflater, ViewGroupcontainer,BundlesavedInstanceState) { • View v = inflater.inflate(R.layout.pix_frag_layout, container, false); • ImageViewmSplashView = (ImageView)v.findViewById(R.id.pix_view); • mSplashView.setOnClickListener(new View.OnClickListener(){ • public void onClick(View v){ • // handle the click • Log.d("PixFragment", "I was clicked"); • } • }); • return v; • } • }