2009-06-15 189 views

回答

5

創建監聽器接口。

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

,並創建另一個活動方法setOnCustomClick(或片段),其中要應用自定義監聽器......

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

呼叫從您的第一項活動這種方法,並通監聽器接口...

89

請務必閱讀observer pattern

監聽器接口

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(); 
    } 
} 
133
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 is invoked when MyButton is clicked 
    @override 
    public void callback(View view, String result) { 
     // do your stuff here 
    } 
} 

public class MyButton { 
    MyListener ml; 

    // constructor 
    MyButton(MyListener ml) { 
     //Setting the listener 
     this.ml = ml; 
    } 

    public void MyLogicToIntimateOthers() { 
     //Invoke the interface 
     ml.callback(this, "success"); 
    } 
} 
+1

如何通過監聽器對象如果我們的按鈕已經在佈局中,而我們不使用MyButton m = new MyButton(this);方法來創建Button的對象。 – 2015-12-07 07:25:24

1

在Android中,你可以創建一個接口,如監聽,你的活動器具它,但我不認爲這是一個好主意。 如果我們有很多組件來監聽它們狀態的變化,我們可以創建一個BaseListener實現接口監聽器,並使用類型代碼來處理它們。 我們可以綁定的方法,當我們創建XML文件,例如:

<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); 
    } 

,但我不認爲這是一個好主意......

8

我已經創建了一個通用AsyncTask監聽器,它從AsycTask獨立類獲得結果,並使用接口回調將其提供給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); 
    } 
} 

看一看thisthis question瞭解更多詳情。

-1

簡單的方法來做到這一點。首先在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; 
     } 
    } 
} 
3

有4個步驟:

1.創建接口類(受聽者)在圖1

2.使用接口(定義變量)

3.implements界面查看2(視圖1中使用的視圖2)

鑑於1

4.pass界面查看2

實施例:

步驟1:你需要創建接口和definde功能

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); 
    } 
} 
相關問題