标签:
事件监听的处理模型包含三个成员:事件源、事件以及事件监听器。
基于监听的事件处理模型一般包含几个步骤:
1、获取普通界面组件;当事件源上发生指定事件的时候,Android就会触发对应事件监听器,并由事件监听器调用相应的方法来处理事件。
public class RemoteControlActivity extends Activity { EditText ipET; EditText socketET; Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ipET = (EditText)findViewById(R.id.IpEditText); socketET = (EditText)findViewById(R.id.SocketEditText); //获取程序中名为 ConnectButton 的按钮组件 button = (Button)findViewById(R.id.ConnectButton); //为按钮绑定事件监听器,并实现该监听类的方法 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String ipnum = ipET.getText().toString(); int socketnum = Integer.parseInt(socketET.getText().toString()); Settings.ipnum =ipnum; Settings.scoketnum = socketnum; try { DatagramSocket socket = new DatagramSocket(); InetAddress serverAddress = InetAddress.getByName(ipnum); Intent intent = new Intent(RemoteControlActivity.this,ControlActivity.class); RemoteControlActivity.this.startActivity(intent); RemoteControlActivity.this.finish(); Toast.makeText(RemoteControlActivity.this, "连接成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }
这里使用匿名内部类作为事件监听器类
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/liuruiqun/article/details/46755381