给返回图标进行点击事件,而后效果是点击返回图标时,可以返回到主界面
AddActivity.java
1 public class AddActivity extends AppCompatActivity { 2 //返回按钮 3 private ImageView mBack; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_add); 9 //返回 10 mBack=findViewById(R.id.back); 11 mBack.setOnClickListener(new View.OnClickListener() { 12 @Override 13 public void onClick(View v) { 14 Toast.makeText(AddActivity.this,"返回成功",Toast.LENGTH_SHORT).show(); 15 Intent intent=null; 16 //返回主页 17 intent=new Intent(AddActivity.this,MainActivity.class); 18 startActivity(intent); 19 } 20 }); 21 22 } 23 24 }