我有一个“简单”的问题:
如何将视图从一个活动传递到另一个活动?
就我而言:
我在这个activity中有一个main activity带有setContentView(R.layout.activity_main);,我声明了一个relativelayout,我想从另一个activity更改它的背景颜色,它从一个.xml文件中获取setContentView,在那里找不到这个relativelayout。我该怎么做?
谢谢你的快速回答!
main活动.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) {


            }
        });
    }

}

最佳答案

简而言之,你不能在两个活动之间通过aView
如果您想根据View中用户的操作修改ActivityOne中的ActivityTwo,那么您应该使用ActivityOne启动ActivityTwo
startActivityForResult()然后可以调用ActivityTwo方法,并且setResult()可以实现ActivityOne来接收该信息。

08-26 03:14