有人能告訴我爲什麼從「啓動新代碼」開始的代碼不起作用? 點擊事件偵聽器中的onClick
方法不會被調用,甚至標籤的文本更改也不會發生。Android Button Click Not Working
日誌顯示按鈕的ID,因此可以找到某物。按鈕和標籤位於LinearLayout
內的TableLayout
。
我綁定按鈕的onCreate方法。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);
setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);
mContactList = (ListView) findViewById(R.id.contactList);
mInputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mApplicationData = (GlobalApplicationData) getApplication();
setTitle(mApplicationData.getAppTitle());
mSettings = mApplicationData.getSettings();
initializedEncryptionKey();
mContactsDB = mApplicationData.getContactsDB();
mXmppModalUI = new XmppModalUI(mInputManager);
mXmppModalUI.initializeModalUI(this, mSettings, this);
mApplicationData.getPersistentXMPP().setModalUI(mXmppModalUI);
mApplicationData.getPersistentXMPP().setContactListCallback(this);
mApprater = new Appirater(this, mSettings);
if (mApplicationData.getUpdateChecker() == null && mApplicationData.getUpdateCheckerUrl() != null) {
mApplicationData.setUpdateChecker(new UpdateChecker(getApplicationContext(), mApplicationData.getUpdateCheckerUrl()));
}
//starting new code
Toast.makeText(getApplicationContext(), "Starting Ali's Code", Toast.LENGTH_SHORT).show();
TableLayout header = (TableLayout) findViewById(R.id.headerLayout);
Log.d("TableLayout",((header == null)?"NOT FOUND":"FOUND "+header.getId()));
final TextView labelHeader = (TextView) header.findViewById(R.id.headerText);
labelHeader.setText("Jump");
final ImageButton btnSettings = (ImageButton) header.findViewById(R.id.btnSettings);
btnSettings.setClickable(true);
Log.d("Settings Button",((btnSettings == null)?"NOT FOUND":"FOUND "+btnSettings.getId()));
btnSettings.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Log.d("OnClick","Here");
// Perform action on clicks
Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_SHORT).show();
startGlobalSettings();
}
});
final ImageButton btnAdd = (ImageButton) header.findViewById(R.id.btnAdd);
btnAdd.setClickable(true);
Log.d("Add Button",((btnAdd == null)?"NOT FOUND":"FOUND "+btnAdd.getId()));
btnAdd.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Log.d("OnClick","Here");
// Perform action on clicks
Toast.makeText(getApplicationContext(), "ADD", Toast.LENGTH_SHORT).show();
showDialog(DialogIds.ADD_CONTACT);
}
});
}
佈局(contact_list.xml):
<TableLayout
android:id="@+id/headerLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow>
<ImageButton
android:id="@+id/btnSettings"
android:gravity="left"
android:padding="3dip"
android:src="@drawable/ic_menu_add"
android:hint="@string/label_settings" />
<TextView
android:id="@+id/headerText"
android:gravity="center"
android:padding="3dip"
android:text="Blank"
android:height="50dip"/>
<ImageButton
android:id="@+id/btnAdd"
android:background="@drawable/disconnectbutton"
android:gravity="right"
android:padding="3dip"
android:src="@drawable/ic_menu_add"
android:hint="@string/label_add" />
</TableRow>
</TableLayout>
<ListView
android:id="@+id/contactList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@color/transparent"
android:cacheColorHint="#00000000" >
</ListView>
UPDATE 我試圖消除header
一部分,事實上,我添加header
部分,因爲它在我簡化之前不工作ly叫findViewById()
如何設置安卓:對XML的onClick?那樣有用嗎? – 2011-12-28 13:32:51