在活动之间传递视图

时间:2017-07-23 16:17:41

标签: android android-intent android-view

我有一个简单的"题: 如何将视图从活动传递到另一个?

就我而言:

我已经使用setContentView(R.layout.activity_main);获得了一个MainActivity在这个activity_main中,我已经声明了一个Relativelayout,我的Backgroundcolor要从另一个Activity更改,它从.xml获取setContentView文件,你找不到这个RelativeLayout。我该怎么做?

感谢您的快速回答!

MainActivity.java

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        if (savedInstanceState != null) {

            //Save the text of the editText
            bringer = savedInstanceState.getString("bringer");

            input.setText(bringer);

            //Save the Listview
            content = savedInstanceState.getStringArrayList("content");

            adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, content);

            layout = (ListView) findViewById(R.id.layout);


            layout.setAdapter(adapter);
        }
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putStringArrayList("content", content);
        outState.putStringArrayList("list", list);

        bringer = input.getText().toString();
        outState.putString("bringer", bringer);
    }

第二项活动:

public class pop extends Activity {

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Thats where it fails, because it couldnt get the parent-
       view.
                finish();
            }
        });

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


            }
        });
    }

}

1 个答案:

答案 0 :(得分:3)

简短的回答是,无法在活动之间传递View

如果View中的ActivityOne要根据用户在ActivityTwo中的操作进行修改,那么您应该ActivityOne启动ActivityTwo使用startActivityForResult()

然后

ActivityTwo可以调用setResult()方法,ActivityOne可以实现onActivityResult()来接收该信息。