现在的位置: 首页 > 综合 > 正文

(5)Handling UI events

2017年12月24日 ⁄ 综合 ⁄ 共 6613字 ⁄ 字号 评论关闭

Handling UI Events via the View Class

An event listener is a Java interface in the View class that contains a single callback method to handle that type of user-input event. When you implement a specific event listener interface, you are telling Android that your
View class will handle that specific event on that specific View.
  

simply implement the nested interface in your activity or define it as an anonymous class within your application. 

final OnClickListener exampleListener = new OnClickListener()
{
public void onClick(View v) {
//Code here that does something upon click event.
}
};

This line of code sets up a variable called exampleListener as a new OnClickListener object, which listens for onClick events.The public onClick(View v) handler to handletheonClick
event. The public onClick handler is passed an ID reference to the View object that was clicked on, so that it knows which View object to handle.

Adding an onClick Listener to a button in Android

connect the button construct with the event listener construct, by using the Button widget’s setOnClickListener() method:

Button exampleButton = (Button)this.findViewById(R.id.firstButton);
exampleButton.setOnClickListener(exampleListener);

Adding an onClick Listener to an Activity in Android:

public class ActivityExample extends Activity implements OnClickListener() {…}

(1)Listener_Handler

/layout/listener_click.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
   <Button 
       android:tag="btn1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="click"
       android:layout_gravity="center"/>
   <TextView
       android:tag="tv1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="before click event"
       android:textColor="#FFCC99"
       android:textSize="24dip" />
  
</LinearLayout>

public class ListenerClick extends Activity implements OnClickListener {
	private View rootView;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		rootView=LayoutInflater.from(this).inflate(R.layout.listener_click, null);
		this.setContentView(rootView);
		
		Button btn=(Button)rootView.findViewWithTag("btn1");
		btn.setOnClickListener(this);
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String tag=(String)v.getTag();
		if("btn1".equals(tag)){
			TextView tv=(TextView)rootView.findViewWithTag("tv1");
			tv.setText("BUTTON HAS BEEN CLICKED. EVENT PROCESSED");
		}
	}
}

(2)OnLongClickListener


public class ListenerClick extends Activity implements OnClickListener,OnLongClickListener {
	private View rootView;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		rootView=LayoutInflater.from(this).inflate(R.layout.listener_click, null);
		this.setContentView(rootView);
		
		Button btn=(Button)rootView.findViewWithTag("btn1");
		btn.setOnClickListener(this);
		btn.setOnLongClickListener(this);
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String tag=(String)v.getTag();
		if("btn1".equals(tag)){
			TextView tv=(TextView)rootView.findViewWithTag("tv1");
			tv.setText("CLICKED.EVENT PROCESSED");
		}
	}
	
	public boolean onLongClick(View v){
		String tag=(String)v.getTag();
		if("btn1".equals(tag)){
			TextView tv=(TextView)rootView.findViewWithTag("tv1");
			tv.setText("LONG CLICKED.EVENT PROCESSED");
		}
		return true;
		
	}
}

(3)Keyboard Event Listeners: onKeyUp and onKeyDown

public class ListenerClick extends Activity implements OnClickListener,OnLongClickListener {
	private View rootView;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		rootView=LayoutInflater.from(this).inflate(R.layout.listener_click, null);
		this.setContentView(rootView);
		
		Button btn=(Button)rootView.findViewWithTag("btn1");
		btn.setOnClickListener(this);
		btn.setOnLongClickListener(this);
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String tag=(String)v.getTag();
		if("btn1".equals(tag)){
			TextView tv=(TextView)rootView.findViewWithTag("tv1");
			tv.setText("CLICKED.EVENT PROCESSED");
		}
	}
	
	public boolean onLongClick(View v){
		String tag=(String)v.getTag();
		if("btn1".equals(tag)){
			TextView tv=(TextView)rootView.findViewWithTag("tv1");
			tv.setText("LONG CLICKED.EVENT PROCESSED");
		}
		return true;
	}
	
	public boolean onKeyDown(int keyCode,KeyEvent event){
		if(keyCode==KeyEvent.KEYCODE_ENTER){
			TextView tv=(TextView)rootView.findViewWithTag("tv1");
			tv.setText("Enter key passed!");
			return true;
		}
		else return false;
	}
}

(4)Context Menus in Android: onCreateContextMenu

The context menu in Android is always accessed by a LongClick event,just as on a PC it is accessed via a right-click.

layout/listener_ctxmenu.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"> 
    <Button 
       android:tag="btn1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="long click"
       android:layout_gravity="center"/> 
</LinearLayout>

public class ListenerContextMenu extends Activity {
	private View rootView;
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		rootView=LayoutInflater.from(this).inflate(R.layout.listener_ctxmenu, null);
		this.setContentView(rootView);
		
		Button ctxBtn=(Button)rootView.findViewWithTag("btn1");
		this.registerForContextMenu(ctxBtn);
	}
	
	public void onCreateContextMenu(ContextMenu menu,View view,ContextMenuInfo menuInfo){
		super.onCreateContextMenu(menu, view, menuInfo);
		menu.setHeaderTitle("Android Context Menu");
		menu.add(0, view.getId(), 0, "Invoke Context Function 1");
		menu.add(0, view.getId(), 0, "Invoke Context Function 2");
	}
	
	public boolean onContextItemSelected(MenuItem item){
		if(item.getTitle().equals("Invoke Context Function 1")){
			this.contextFunction(item.getItemId());
		}else if(item.getTitle().equals("Invoke Context Function 2")){
			this.contextFunction(item.getItemId());
		}else{
			return false;
		}
		return true;
	}
	
	private void contextFunction(int id){
		Toast.makeText(this, "function is invoked"+id, Toast.LENGTH_LONG).show();
	}
}

(5) Focus Control

<LinearLayout 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">
<Button 
   android:id="@+id/contextButton"
   android:text="First Button"
android:nextFocusUp="@+id/thirdButton"
android:nextFocusDown="@+id/secondButton"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button 
   android:id="@+id/secondButton"
   android:text="Second Button"
android:nextFocusUp="@+id/contextButton"
android:nextFocusDown="@+id/thirdButton"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button 
   android:id="@+id/thirdButton"
   android:text="Third Button"
android:nextFocusUp="@+id/secondButton"
android:nextFocusDown="@+id/contextButton"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

抱歉!评论已关闭.