将数据从内部视图传递到 Activity
Pass data from inner View to Activity
我有一个包含视图的 activity。
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_act);
LinearLayout surface = (LinearLayout) findViewById(R.id.surfaceView1);
surface.addView(new PlacingBoxView(this));
surface.setBackgroundColor(Color.BLACK);
if (ACTIONS_FINISHED){
Log.d("TAG", "i made it!!");
}
}
在 PlacingBoxView
中,我执行了一些操作,完成后我将一个名为 ACTIONS_FINISHED
的布尔值设置为 true。我的目标是一旦我将此布尔值设置为 true,然后自动在 activity 中执行一些操作。
我一直在尝试使用 all this possibilities,但它们只是为了共享信息。也许我应该使用 onActivityResult,但我已经在 activity...
有什么想法吗?提前致谢!
编辑: 视图执行 AsyncTask 并绘制多个形状,但用户根本不触摸屏幕
I've been trying to use all this possibilities, but they are just to share information. Maybe I should use onActivityResult, but I'm already within the activity...
不,onActivityResult()
有不同的目的。你应该做的是在你的视图中实现普通的监听器,然后将你的 activity 附加到视图的监听器(与你对 OnClickListener
所做的方式相同)。这样您的视图将能够在需要时回调并以这种方式触发 Activity 中的一些操作。
在您的 PlacingBoxView
class:
中定义一个接口
public class PlacingBoxView{
private GetCallBack callback;
public PlacingBoxView(Context context){
callback = (GetCallBack) context;
}
public void someMethodWhichSendsData(){
callback.onMessageReceived(msg);
}
public interface GetCallBack{
void onMessageReceived(Object msg);
}
}
现在在您的 activity
中实现接口:
public class SecondActivity extends AppCompatActivity implements PlacingBoxView.GetCallBack{
@Override
public void onMessageReceived(Object msg){
// you have got your msg here.
}
}
我有一个包含视图的 activity。
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_act);
LinearLayout surface = (LinearLayout) findViewById(R.id.surfaceView1);
surface.addView(new PlacingBoxView(this));
surface.setBackgroundColor(Color.BLACK);
if (ACTIONS_FINISHED){
Log.d("TAG", "i made it!!");
}
}
在 PlacingBoxView
中,我执行了一些操作,完成后我将一个名为 ACTIONS_FINISHED
的布尔值设置为 true。我的目标是一旦我将此布尔值设置为 true,然后自动在 activity 中执行一些操作。
我一直在尝试使用 all this possibilities,但它们只是为了共享信息。也许我应该使用 onActivityResult,但我已经在 activity...
有什么想法吗?提前致谢!
编辑: 视图执行 AsyncTask 并绘制多个形状,但用户根本不触摸屏幕
I've been trying to use all this possibilities, but they are just to share information. Maybe I should use onActivityResult, but I'm already within the activity...
不,onActivityResult()
有不同的目的。你应该做的是在你的视图中实现普通的监听器,然后将你的 activity 附加到视图的监听器(与你对 OnClickListener
所做的方式相同)。这样您的视图将能够在需要时回调并以这种方式触发 Activity 中的一些操作。
在您的 PlacingBoxView
class:
public class PlacingBoxView{
private GetCallBack callback;
public PlacingBoxView(Context context){
callback = (GetCallBack) context;
}
public void someMethodWhichSendsData(){
callback.onMessageReceived(msg);
}
public interface GetCallBack{
void onMessageReceived(Object msg);
}
}
现在在您的 activity
中实现接口:
public class SecondActivity extends AppCompatActivity implements PlacingBoxView.GetCallBack{
@Override
public void onMessageReceived(Object msg){
// you have got your msg here.
}
}