如何在android中创build我们自己的Listener接口?

有人可以帮助我创build用户定义的监听器接口与一些代码片段?

public interface MyListener { // you can define any parameter as per your requirement public void callback(View view, String result); } public class MyActivity extends Activity implements MyListener { @override public void onCreate(){ MyButton m = new MyButton(this); } // method invoke when mybutton will click @override public void callback(View view, String result) { // do your stuff here } } public class MyButton { MyListener ml; // constructor MyButton(MyListener ml) { this.ml = ml; } public void MyLogicToIntimateOthere() { ml.callback(this, "success"); } } 

请阅读观察者模式

监听器接口

 public interface OnEventListener { void onEvent(EventResult er); // or void onEvent(); as per your need } 

然后在你的课堂上说Event

 public class Event { private OnEventListener mOnEventListener; public void setOnEventListener(OnEventListener listener) { mOnEventListener = listener; } public void doEvent() { /* * code code code */ // and in the end if (mOnEventListener != null) mOnEventListener.onEvent(eventResult); // event result object :) } } 

在你的驱动程序类MyTestDriver

 public class MyTestDriver { public static void main(String[] args) { Event e = new Event(); e.setOnEventListener(new OnEventListener() { public void onEvent(EventResult er) { // do your work. } }); e.doEvent(); } } 

我创build了一个通用的AsyncTask Listener,它从AsycTask独立类获得结果,并使用接口callback将其提供给CallingActivity。

 new GenericAsyncTask(context,new AsyncTaskCompleteListener() { public void onTaskComplete(String response) { // do your work. } }).execute(); 

接口

 interface AsyncTaskCompleteListener<T> { public void onTaskComplete(T result); } 

GenericAsyncTask

 class GenericAsyncTask extends AsyncTask<String, Void, String> { private AsyncTaskCompleteListener<String> callback; public A(Context context, AsyncTaskCompleteListener<String> cb) { this.context = context; this.callback = cb; } protected void onPostExecute(String result) { finalResult = result; callback.onTaskComplete(result); } } 

看看这个这个问题的更多细节。

创build监听器接口。

 public interface YourCustomListener { public void onCustomClick(View view); // pass view as argument or whatever you want. } 

并创build方法setOnCustomClick在另一个活动(或片段),你想要应用您的自定义侦听器……

  public void setCustomClickListener(YourCustomListener yourCustomListener) { this.yourCustomListener= yourCustomListener; } 

从您的第一个活动调用此方法,并传递侦听器接口…

在Android中,你可以创build一个接口,比如Listener,而你的Activity实现它,但是我不认为这是个好主意。 如果我们有很多组件来监听它们状态的变化,我们可以创build一个BaseListener实现接口Listener,并使用types代码来处理它们。 我们可以在创buildXML文件时绑定这个方法,例如:

 <Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button4" android:onClick="Btn4OnClick" /> 

和源代码:

  public void Btn4OnClick(View view) { String strTmp = "点击Button04"; tv.setText(strTmp); } 

但我不认为这是一个好主意…

有4个步骤:

1.创build接口类(listener)

2.使用界面视图1(定义variables)

3.implements界面查看2(视图1中使用的视图1)

4.在view 1中通过界面查看2

例:

第一步:您需要创build界面和定义function

 public interface onAddTextViewCustomListener { void onAddText(String text); } 

第2步:使用该界面

 public class CTextView extends TextView { onAddTextViewCustomListener onAddTextViewCustomListener; //listener custom public CTextView(Context context, onAddTextViewCustomListener onAddTextViewCustomListener) { super(context); this.onAddTextViewCustomListener = onAddTextViewCustomListener; init(context, null); } public CTextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context, attrs); } public CTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public CTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(context, attrs); } public void init(Context context, @Nullable AttributeSet attrs) { if (isInEditMode()) return; //call listener onAddTextViewCustomListener.onAddText("this TextView added"); } } 

步骤3,4:执行到活动

 public class MainActivity extends AppCompatActivity implements onAddTextViewCustomListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //get main view from layout RelativeLayout mainView = (RelativeLayout)findViewById(R.id.mainView); //create new CTextView and set listener CTextView cTextView = new CTextView(getApplicationContext(), this); //add cTextView to mainView mainView.addView(cTextView); } @Override public void onAddText(String text) { Log.i("Message ", text); } } 

简单的方法来做这个方法。 首先在Activity类中实现OnClickListeners

码:

 class MainActivity extends Activity implements OnClickListeners{ protected void OnCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity_main.xml); Button b1=(Button)findViewById(R.id.sipsi); Button b2=(Button)findViewById(R.id.pipsi); b1.SetOnClickListener(this); b2.SetOnClickListener(this); } public void OnClick(View V) { int i=v.getId(); switch(i) { case R.id.sipsi: { //you can do anything from this button break; } case R.id.pipsi: { //you can do anything from this button break; } } }