2012-08-28 31 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 –