Android Sutdio:两个Activity活动的响应
一: 在XML文件文件中设置好按钮(组件id是身份,我用的是相对布局)
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进入!"
android:textSize="22dp"
android:background="#E9E3E3"
android:id="@+id/bt_01"
android:layout_marginTop="20dp"
android:layout_below="@id/im3"
android:layout_centerHorizontal="true" />
修改对应的MainActivity.java文件:
public class MainActivity extends AppCompatActivity {
private View.OnClickListener bt_02_Click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_01 = (Button)findViewById(R.id.bt_01);
bt_01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1 = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent1);
}
});
--------------------------------(完成以上既可运行)----------------------------------------- 拓展:同样可以设置复选框(checkbox)的响应,编辑视图(editview)等的响应(类似)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox1);
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent2 = new Intent(Main2Activity.this,Main3Activity.class);
startActivity(intent2);
}
});
}
二:可以设置按钮响应提示文本(不跳转第二个活动) (修改活动的java文件)–不是调用Intent类而是Toast类
public class MainActivity extends AppCompatActivity {
private View.OnClickListener bt_02_Click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_02 = (Button)findViewById(R.id.bt_02);
bt_02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"退出失败!",Toast.LENGTH_LONG).show();
}
});
-----------------------------------------可以运行就不截图了!!-------------------------------------
提示:如若java文件里面类报红,按alt +enter 导入对应的类
|