2012-08-28 88 views
0

我有一個小的查詢, 假設使用意圖擷取佈局細節

startActivity(intent); 

和一個新的屏幕打開了它的內容,如按鈕和文本字段,我們開始活動 我只需要知道是否有可能獲取關於佈局及其內容的細節。

我認爲它不可能,但只是想得到一個適當的確認。

+0

你需要從第二個佈局得到什麼細節,爲什麼? – Mohit

+0

我需要從第二個佈局自動按下按鈕,這讓我思考。現在它更多地出於好奇,我會找到一種不同的方法,但仍然只是想知道它是否可能是 – Ajay

回答

1

不,這是不可能做到這一點。並且(從評論)不,從單獨的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 
    } 
} 

你提供一個整數開始ActivityIntent。該整數表示View(更具體地說,是Button)。一旦Activity收到它,它會找到匹配的View,檢查其是否爲Button,然後對其執行點擊操作。

這樣,你只是傳遞一個項目的整數ID點擊,而你打開的Activity只是處理其餘的。

+0

雅,這實際上是好的,但我不能使用它,我可以使用意圖添加事件到日曆,但對於自定義活動,這實際上將工作得很好 – Ajay

+0

爲什麼是這樣的? – Eric

+0

@Eric你能解釋一下你的代碼嗎?我喜歡這個概念,但並不真正瞭解它。 – Mohit

0

你可以使用getChildAt()的內容

for(int i=0; i<((ViewGroup)v).getChildCount(); ++i) { 
    View nextChild = ((ViewGroup)v).getChildAt(i); 
} 
+1

AFAIK,這隻適用於當前的「活動」。 OP希望進入另一個「Activity」的佈局細節。 – Eric

+0

你能詳細解釋一下嗎? – Ajay

+0

謝謝澄清。@ Eric –