Android 实现监听 Android 实现监听的四种方法详解实例代码
W川秋水 人气:3想了解Android 实现监听的四种方法详解实例代码的相关内容吗,W川秋水在本文为您仔细讲解Android 实现监听的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,实现监听,Android,实现监听的方法,Android,实现监听方法总结,下面大家一起来学习吧。
直接上代码,大家可以参考下
(1)自身类作为事件监听器
package cn.edu.gdmec.s07150745.work5; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn1 = (Button) findViewById(R.id.btn1); btn1.setText("单击"); btn1.setOnClickListener(this); } @Override public void onClick(View v) { Toast.makeText(this,"已单击我...",Toast.LENGTH_SHORT).show(); System.out.println("已单击我..."); } }
(2)外部类作为事件监听器:
package cn.edu.gdmec.s07150745.work5; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Act2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act2); Button btn2 = (Button) findViewById(R.id.btn2); btn2.setText("单击"); btn2.setOnClickListener(new OuterClass("已单击我...")); } } class OuterClass implements View.OnClickListener{ private String str="已单击我...."; public OuterClass(String str){ super(); this.str=str; } @Override public void onClick(View v) { System.out.println(str); } }
(3)内部类作为事件监听器:
package cn.edu.gdmec.s07150745.work5; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Act3 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act3); Button btn3 = (Button) findViewById(R.id.btn3); btn3.setText("单击"); btn3.setOnClickListener(new OuterClass()); } class OuterClass implements View.OnClickListener{ @Override public void onClick(View v) { Toast.makeText(Act3.this,"已单击我...",Toast.LENGTH_SHORT).show(); System.out.println("已单击我..."); } } }
(4)匿名类作为事件监听器:
package cn.edu.gdmec.s07150745.work5; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Act4 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_act4); Button btn4 = (Button) findViewById(R.id.btn4); btn4.setText("单击"); btn4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(Act4.this,"已单击我...",Toast.LENGTH_SHORT).show(); System.out.println("已单击我..."); } }); } }
以上四种方法之后,在xml文件中给每一个事件即activity里面定义多一个android label即可。不忘在每个activity里面添加如下蓝色的一段代码哦。
<activity android:name=".MainActivity" android:label="1"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
加载全部内容