Android - transaction processing (12)

1. Introduction to event processing

1.1 Knowledge points

(1) Understand the role of event processing;

(2) Understand commonly used events and related processing interfaces;

1.2 Specific details

In Android, basically every component has corresponding event processing, but no matter how many types of events and components there are, the event operation process is the same. Event processing requires an event source, and the event source includes the component that triggers the event itself. and the type of event are two key pieces of information. Then every time an event is triggered, there needs to be a corresponding listener for processing.

 

 

package com.example.eventlistenerproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class EventListenerActivity extends Activity {
       Button but = null;
       EditText edt = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_listener);
        but = (Button)super.findViewById(R.id.but);
        edt = (EditText)super.findViewById(R.id.edt);
        but.setOnClickListener(new OnClickListenerImpl());
        
        
    }
  class OnClickListenerImpl implements OnClickListener{

	@Override
	public void onClick(View v) {
		edt.setText("按钮被点击了");
		
	}}

   
}

 The above is the form of declaring an inner class, and we have achieved the effect of registering a click event.

Use anonymous inner classes:

package com.example.eventlistenerproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class EventListenerActivity extends Activity {
       Button but = null;
       EditText edt = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_listener);
        but = (Button)super.findViewById(R.id.but);
        edt = (EditText)super.findViewById(R.id.edt);
        but.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				edt.setText("按钮被点击了,这次是使用的匿名内部类");
				
			}
		});
        
        
    }
  
   
}

At the same time, you can also register it directly in the layout file. Correspondingly, you only need to declare an event processing method in the Activity with the same name as the registered method (it should be noted that this method also needs a View object as parameter).

1.3 Summary

The core of event processing lies in the following operations:

(1) Register the monitoring program;

(2) Write the specified handler based on the specified event;

(3) Complete event processing operations in the event processing class.

2. Click event

2.1 Knowledge points

(1) Master the use and processing of click events;

(2) Use click events to complete the development of common operating procedures.

2.2 Specific details

The processing interface for handling click events is OnClickListener, and the method that needs to be overridden is the OnClick method.

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".OnClickActivity" >
    
    <EditText
          android:id="@+id/edt"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
           />
    
    <Button 
          android:id="@+id/but"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="显示输入的内容" 
        />
    
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />

</LinearLayout>

 

package com.example.onclickproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class OnClickActivity extends Activity {
    EditText edt = null;
    TextView tv = null;
    Button but = null;
	 
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_on_click);
        this.edt = (EditText) super.findViewById(R.id.edt);
        this.but = (Button) super.findViewById(R.id.but);
        this.tv  = (TextView) super.findViewById(R.id.tv);
        this.but.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String inputText = edt.getText().toString();//取得文本输入的内容
				tv.setText(inputText);
			}
		});
    }


   
}

The function completed by the above program is to display the content entered in EditText into TextView. Here we review the processing process of the click event again.

2.3 Summary

(1) The click event is a common event defined by View itself;

(2) When using click events, you can use internal classes or anonymous internal classes for event processing.

3. Radio buttons and OnCheckedChangeListener

3.1 Knowledge points

(1) Use the option change event in the radio button to operate.

3.2 Specific details

Example: Put the value of the radio option selected by the user into TextView to display

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".OnCheckedChangeListenerActivity" >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择性别:" />
    <RadioGroup 
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:checkedButton="@+id/male"
        
        >
        <RadioButton 
            android:id="@+id/male"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="男"
            />
        <RadioButton 
            android:id="@+id/female"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="女"
            />
        
        
        
    </RadioGroup>

</LinearLayout>

 

package com.example.oncheckedchangerlistenerproject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class OnCheckedChangeListenerActivity extends Activity {
    RadioGroup rg = null;
    RadioButton male =null;
    RadioButton female = null;
    TextView tv =null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_on_checked_change_listener);
        rg = (RadioGroup) super.findViewById(R.id.rg);
        male = (RadioButton) super.findViewById(R.id.male);
        female = (RadioButton) super.findViewById(R.id.female);
        tv = (TextView) super.findViewById(R.id.tv);
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				String str = null;
				if(male.getId()==checkedId){
					tv.setText("选中的性别是男");
				}else{
					tv.setText("选中的性别是女");
				}
				
			}
		});
    }


    
}

If there are multiple options here, the judgment needs to be made multiple times.

The difference between internal class registration monitoring and anonymous internal class registration monitoring: The most obvious difference is to see whether this event is only executed once.

If there are multiple event listeners of the same type in an Activity, then we use internal class registration to register these listeners in a unified manner based on multiple event source judgments. In this case, use internal classes It is obviously more appropriate to save code and make the code easier to read. If it is just a single event (only used once to listen) then we can choose an anonymous inner class.

3.3 Summary

(1) The OnCheckedChange event can be used to monitor when options change, using .

(2) The OnCheckedChangeListener interface can complete event monitoring and processing.

4. Drop-down list box and OnItemSelectedListener

4.1 Knowledge points

(1) Use event listening to operate the Spinner component;

(2) Complete the development of cascading menus.

4.2 Specific details

 

The drop-down list has some monitoring operations, and the corresponding monitoring registration methods are:

       · Click option: setOnItemClickListener

       · Option change: setOnItemSelectListener

       · Long press event: setOnItemLongClickListener

Example: Write an example of linkage between provinces and cities

<?xml version="1.0" encoding="utf-8"?>
<resources>
       <string-array name="province">
           <item>甘肃</item>
           <item>山西</item>
           <item>宁夏</item>
       </string-array>
</resources>

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">下拉列表监听</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="selectPro">请选择省份</string>
    <string name="selectCity">请选择城市</string>
</resources>

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".SpinnerListenerActivity" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择所在省份" />
    <Spinner 
        android:id="@+id/province"
        android:prompt="@string/selectPro"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/province"
        />
    <Spinner 
        android:id="@+id/city"
        android:prompt="@string/selectCity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
        />
</LinearLayout>

package com.example.spinnerlistenerproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class SpinnerListenerActivity extends Activity {
    Spinner province,city = null;
    String s[][] = new String[][]{
   
   {"兰州","定西","武威","酒泉"},
    		{"西安","咸阳","榆林","宝鸡"},{"银川","吴忠","石嘴山","乌海"}};
    ArrayAdapter<CharSequence> array = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_listener);
        this.province = (Spinner) super.findViewById(R.id.province);
        this.city = (Spinner) super.findViewById(R.id.city);
        this.province.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				array = new ArrayAdapter<CharSequence>(SpinnerListenerActivity.this, 
						android.R.layout.simple_spinner_item,s[arg2]);
				array.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				city.setAdapter(array);//给下拉列表设置选项
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// 没有这个需求,就什么都不做,但是,由于这是来自接口的方法,所有我们必须这里覆写一下
				
			}
		});
    }


    
}

4.3 Summary

(1) The drop-down list can be monitored using the OnItemSelectedListener interface;

(2) When using the cascading menu, you only need to reset the content of the submenu.

5. Monitor date and time changes

5.1 Knowledge points

(1) Master event processing operations of date and time;

(2) Event dynamics can be used to obtain the modified date and time.

5.2 Specific details

Example: Display the date in the text component in the form of yyyy-MM-dd HH:mm. As long as the date or time changes, the content in the text component changes accordingly.

<TableLayout 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=".DateAndTimeListenerActivity" >

    <TableRow 
        android:orientation="vertical"
        >
        <EditText
            android:id="@+id/edt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="此处显示时间日期"
            />
        
    </TableRow>
    
    
    <TableRow 
        android:orientation="horizontal"
        >
        <DatePicker
            android:id="@+id/dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <TimePicker 
            android:id="@+id/tp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        
    </TableRow>

</TableLayout>

 

package com.example.dateandtimelistener;

import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class DateAndTimeListenerActivity extends Activity {
    DatePicker dp = null;
    TimePicker tp =null;
    EditText edt = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_and_time_listener);
        this.dp = (DatePicker) super.findViewById(R.id.dp);
        this.tp = (TimePicker) super.findViewById(R.id.tp);
        this.edt = (EditText) super.findViewById(R.id.edt);
        this.tp.setIs24HourView(true);
        //特别注意下,DatePicker监听的注册和其他的组件有不同
        this.dp.init(this.dp.getYear(),this.dp.getMonth(),this.dp.getDayOfMonth(),new OnDateChangedListener() {
			
			@Override
			public void onDateChanged(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				initTime();
				
			}
		});
        this.tp.setOnTimeChangedListener(new OnTimeChangedListener() {
			
			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				initTime();
				
			}
		});
    }
   
    public void initTime(){//此方法用于修改编辑框中的内容
    	String input = null;
    	input = this.dp.getYear()+"-"+(this.dp.getMonth()+1)+"-"+this.dp.getDayOfMonth()+" "+this.tp.getCurrentHour()
    			    +":"+this.tp.getCurrentMinute();
    	this.edt.setText(input);
    }

    
}

5.3 Summary

(1) The listening interface of the date picker is: android.widget.DatePicker.OnDateChangedListener;

(2) The listening interface of the time picker is: android.widget.TimePicker.OnTimeChangedListener;

6. Focus event

6.1 Knowledge points

(1) Master focus operations and related event processing operations.

6.2 Specific details

Example: Describes the processing of focus events. When the text input box loses focus, the content in the text input box is displayed.

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".OnFocusListenerActivity" >

    <EditText 
        android:id="@+id/edt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请输入内容"
        />
    <TextView 
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是内容"
        />
    <EditText 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是焦点"
        />
</LinearLayout>

 

package com.example.onfocuslistenerproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.TextView;

public class OnFocusListenerActivity extends Activity {
    EditText edt = null;
    TextView tv = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_on_focus_listener);
        this.edt = (EditText) super.findViewById(R.id.edt);
        this.tv = (TextView) super.findViewById(R.id.tv);
        edt.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				
				if(hasFocus){//得到焦点
					edt.setText("");
				}else{
				    tv.setText(edt.getText());	
				}
			}
		});
    }


   
    
}

6.3 Summary

(1) Focus events are mainly processed when a component gains or loses focus.

Guess you like

Origin blog.csdn.net/weixin_41830242/article/details/131280303