2009-06-15 7 views

Odpowiedz

5

Utwórz interfejs nasłuchujący.

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

I stworzyć metodę setOnCustomClick w innej aktywności (lub jego fragment), w którym chcesz zastosować niestandardową słuchacza ......

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

wywołać tę metodę z pierwszą czynnością, i przekazać interfejs słuchacz ...

89

proszę przeczytać observer pattern

interfejs słuchacz

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

następnie w klasie mówią Event klasa

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

w swojej klasie kierowcy 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

Jak przekazać obiekt Listener jeśli nasz Button jest już w układzie raczej nie użył myButton m = new myButton (this); sposób na stworzenie obiektu Button. –

+2

Możesz dodać nową metodę w klasie MyButton: void setMyListener (MyListner m1) {this.ml = m1;} , a następnie użyj tej metody w dowolnym momencie, aby ustawić obiekt detektora. –

+1

gdzie jest MyLogicToIntimateOthere() ta metoda używana? – abh22ishek

1

W Androidzie można utworzyć interfejs, takich jak słuchacza, a swoimi narzędziami działalności ale nie sądzę, że to dobry pomysł. jeśli mamy wiele komponentów do nasłuchu zmian ich stanu, możemy stworzyć BaseListener implementujący interfejs nasłuchiwania i użyć kodu typu do ich obsługi. możemy powiązać metodę podczas tworzenia pliku XML, na przykład:

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

i kodu źródłowego:

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

ale nie sądzę, że jest to dobry pomysł ...

8

Utworzono Generic AsyncTask Listener, który otrzymuje wynik z oddzielnej klasy AsycTask i przekazuje go do CallingActivity za pomocą interfejsu Callback.

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

Interfejs

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

Wystarczy popatrzeć na this, this question więcej szczegółów.

-1

Prosta metoda na takie podejście. Najpierw implementuje OnClickListeners w twojej klasie aktywności.

Kod:

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

Istnieją 4 etapy:

klasy interfejs 1.Create (detektora) Interfejs

2.Wykorzystanie w związku 1 (określenie zmiennej)

3. Interfejs do podglądu 2 (widok 1 używany w widoku 2)

interfejs

4.pass w widoku od 1 do 2 Widok

przykład:

Etap 1: trzeba utworzyć interfejs i funkcji definde

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

Etap 2: ten interfejs się zobacz

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

Krok 3,4: implementuje do działania

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); 
    } 
} 
Powiązane problemy