我有一個小的查詢, 假設使用意圖擷取佈局細節
startActivity(intent);
和一個新的屏幕打開了它的內容,如按鈕和文本字段,我們開始活動 我只需要知道是否有可能獲取關於佈局及其內容的細節。
我認爲它不可能,但只是想得到一個適當的確認。
我有一個小的查詢, 假設使用意圖擷取佈局細節
startActivity(intent);
和一個新的屏幕打開了它的內容,如按鈕和文本字段,我們開始活動 我只需要知道是否有可能獲取關於佈局及其內容的細節。
我認爲它不可能,但只是想得到一個適當的確認。
不,這是不可能做到這一點。並且(從評論)不,從單獨的Activity
「單擊」按鈕是不可能的。你可以做什麼,但是,是這樣的:
// Calling code:
intent.putExtra(getPackageName() + ".click_me", R.id.your_button); // getPackageName() is for best practices
startActivity(intent);
// In your Activity:
Intent intent = getIntent(); // Get the Intent we used to launch this
int buttonToClick = intent.getIntExtra(getPackageName() + ".click_me", 0); // Get the integer
if (buttonToClick != 0) { // If it's 0, we didn't specify it
View toClick = findViewById(buttonToClick); // Find the View
if (toClick != null && toClick instanceof Button) { // If the View exists, and is a Button..
((Button) toClick).performClick(); // ..then click it
}
}
你提供一個整數開始Activity
的Intent
。該整數表示View
(更具體地說,是Button
)。一旦Activity
收到它,它會找到匹配的View
,檢查其是否爲Button
,然後對其執行點擊操作。
這樣,你只是傳遞一個項目的整數ID點擊,而你打開的Activity
只是處理其餘的。
你需要從第二個佈局得到什麼細節,爲什麼? – Mohit
我需要從第二個佈局自動按下按鈕,這讓我思考。現在它更多地出於好奇,我會找到一種不同的方法,但仍然只是想知道它是否可能是 – Ajay