我試過使用this鏈接,但我無法讓它適用於微調和單選按鈕。Android - 如何在微調器和單選按鈕之間共享監聽器?
是否有更好或更簡單的方式在不同品種的不同項目之間共享聽衆?
編輯 - 我應該提到,我想分享聽衆的原因是我需要使用各種適配器,我在其他類中遇到了麻煩。
我試過使用this鏈接,但我無法讓它適用於微調和單選按鈕。Android - 如何在微調器和單選按鈕之間共享監聽器?
是否有更好或更簡單的方式在不同品種的不同項目之間共享聽衆?
編輯 - 我應該提到,我想分享聽衆的原因是我需要使用各種適配器,我在其他類中遇到了麻煩。
可以定義實現監聽兩個微調和單選按鈕類,
創建類的一個實例,然後將該實例分配到兩個單選按鈕和微調。 例如:
package italialinux.example;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TextView;
public class ButtonAction implements OnClickListener, OnItemSelectedListener {
TextView btnLocalText;
public ButtonAction(TextView tv) {
super();
btnLocalText = tv;
}
@Override
public void onClick(View arg0) {
btnLocalText.setText("Hello from a ButtonAction!!");
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
如果你希望他們在點擊執行相同的功能,只需在setOnClickListener()方法中添加相同聽者實例每個視圖/小部件。如果沒有,那麼你將要檢測被單擊視圖和執行所需的操作相應
我想下面的代碼示例是你在尋找什麼
//the import for onClickListener you need is here (along with other imports --- you can get all of them by pressing ctrl+shift+o on Eclips IDE)
進口android.view.View.OnClickListener;
公共類MyActivity擴展活動實現OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button myButton = (Button) findViewById(R.id.myButtonId);
ImageView myImageView = (ImageView) findViewById(R.id.myImageViewId);
RadioButton myRadioButton = (RadioButton) findViewById(R.id.myRadioButtonId);
CheckBox myCheckBox = (CheckBox) findViewById(R.id.myCheckBoxId);
myButton.setOnClickListener(this);
myImageView.setOnClickListener(this);
myRadioButton.setOnClickListener(this);
myCheckBox.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.myButtonId:
// do the work here for Button click listener
break;
case R.id.myImageViewId:
// do the work here for Image click listener
break;
case R.id.myRadioButtonId:
// do the work here for RadioButton click listener
break;
case R.id.myCheckBoxId:
// do the work here for CheckBox click listener
break;
}
}
}
不幸的是,它只是讓我的程序崩潰。感謝您的嘗試! – KRL
的問題是,我需要使用我在使用其他類的麻煩各種適配器。 – KRL
這不起作用? – Ivan
您在那裏添加的奇妙示例,謝謝。對於其他人來說,像我自己一樣,這是需要添加到你的按鈕/旋鈕/ misc來調用課程; clr.setOnClickListener(new ButtonAction('whatever value')); – KRL