Intent

百度经验

public class MainActivity extends AppCompatActivity {

EditText editText;

String name = "";
int color = Color.WHITE;
//列表
ArrayListarrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = findViewById(R.id.editText);

    arrayList.add(new Person("ft",22));
    arrayList.add(new Person("zn",22));
    arrayList.add(new Person("zhm",22));

    restore();//重新构造的时候恢复
}

public void switchName(View v) {

    //定义意图
    Intent intent = new Intent(this,Main2Activity.class);
    intent.putExtra("persons",arrayList);
    //正向传参
    //startActivity(intent);

    //请求码
    startActivityForResult(intent,1);

}

public void switchColor(View v) {

    //定义意图
    Intent intent = new Intent(this,Main3Activity.class);

    //请求码
    startActivityForResult(intent,2);

}

//反向传回参数
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode)
    {
        case 1:
            name = data.getStringExtra("name");
            editText.setText(name);
            break;
        case 2:
            color = data.getIntExtra("color",Color.RED);
            View v = findViewById(R.id.layout);
            v.setBackgroundColor(color);
            break;
    }
}

@Override
protected void onPause() {
    super.onPause();
    save();
}

//保存状态
public void save(){
    //保存数据SharedPreferences(只能存简单的字符,数组)
    SharedPreferences sharedPreferences = getSharedPreferences("mypre",MODE_PRIVATE);//随便给的名字mypre
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("name",name);
    editor.putInt("color",color);
    editor.commit();
}

//恢复
public void restore(){
    SharedPreferences sharedPreferences = getSharedPreferences("mypre",MODE_PRIVATE);//随便给的名字mypre
    name = sharedPreferences.getString("name","");
    color = sharedPreferences.getInt("color",Color.WHITE);
    editText.setText(name);
    View v = findViewById(R.id.layout);
    v.setBackgroundColor(color);
}

}

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    TextView textView = findViewById(R.id.textView);
    ListView listView = findViewById(R.id.listView);

    final ArrayListarrayList =(ArrayList) getIntent().getSerializableExtra("persons");
    ArrayAdapter arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,arrayList);
   listView.setAdapter(arrayAdapter);

   listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView parent, View view, int position, long id) {
           Person person = arrayList.get(position);
           Intent intent = new Intent();
           intent.putExtra("name",person.getName());
           setResult(1,intent);//1代表传值成功
           finish();
       }
   });
}

}

public class Main3Activity extends AppCompatActivity {

int []colors = {Color.RED,Color.GREEN,Color.BLUE,Color.GRAY};
String []string_colors = {"RED","GREEN","BLUE","GRAY"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);

    ListView listView = findViewById(R.id.ListView);
    ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,string_colors);
    listView.setAdapter(arrayAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            int color = colors[position];//拿到颜色
            Intent intent = new Intent();
            intent.putExtra("color",color);
            setResult(0,intent);
            finish();//结束界面
        }
    });
}

}

你可能感兴趣的:(Intent)