【发布时间】:2013-08-02 23:29:58
【问题描述】:
我有一个自定义 xml 视图,当点击按钮时,会在主视图中创建一个副本。
Button addButton = (Button) findViewById(R.id.btnAdd);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout newCard = (LinearLayout) getLayoutInflater().inflate(R.layout.vehicle, null);
cardLayout.addView(newCard);
final int thisCarId = new Random().nextInt(10000);
}
});
用户可以根据需要多次点击 addButton,我每次都会生成一个新的 newCard。
每个 newCard 都有一个 ImageButton 和一个 EditText;当点击 ImageButton 时,我会使用 Result 启动相机意图:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, thisCarId);
稍后在这个活动类中,我预计相机活动的结果:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v("car", "The ID that was returned: " + requestCode);
}
我的日志确实通过 thisCardId 正确确定了哪个 newCard 实例...但是我如何知道创建了哪个 newCard,以便我可以设置在该 newCard 布局中找到的 ImageButton 以匹配相机照片?分配照片没问题...只是,我不知道分配哪个,因为onActivityResult超出范围,据我所知,我无法为活动分配匿名Override作为一个整体...
【问题讨论】:
-
我不太确定我是否理解您要执行的操作,但来自 “我无法为整个活动分配匿名覆盖...” 您的意思是将
OnClickListener设置为Activity?如果是这样,只需让您的Activity实现View.OnClickListener接口。 -
对不起,文字不是我的强项;我的意思是设置一个匿名 onActivityResult 专用于 newCard 的实例。
标签: android view android-activity copy onactivityresult