跳转到另一个Activity"/>
Android Studio 点击按钮跳转到另一个Activity
there
- 一、前言
- 二、前置工作
- 2.1创建新的界面和按钮
- 2.2、绑定按钮
- 三、intent进行连接并跳转
- 四、总结
一、前言
最近才开始使用Android Studio,不太熟悉,用博客记录一下。
二、前置工作
2.1创建新的界面和按钮
创建一个新的empty activity,并且在新创建的activity的配置文件里配置按钮,也就是xml文件。
配置按钮的代码:
<Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="方式一"android:id="@+id/type1"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="方式二"android:id="@+id/type2"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="方式三"android:id="@+id/type3"/>
2.2、绑定按钮
在mainactivity里创建几个按钮对象,并且将按钮对象与按钮示例绑定,
代码:
Button btn_type1;Button btn_type2;Button btn_type3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn_type1=(Button) findViewById(R.id.type1);btn_type2=(Button) findViewById(R.id.type2);btn_type3=(Button) findViewById(R.id.type3);btn_type1.setOnClickListener(this);btn_type2.setOnClickListener(this);btn_type3.setOnClickListener(this);}
三、intent进行连接并跳转
显示Intent和隐式Intent的用法不一样,隐式的话步骤要多一步,要在AndroidManifest.xml里要用的activity里加入以下代码,action那里可以随便改,只要后面引用的时候与之对应就行,category那里是一个默认参数
<activityandroid:name=".dateActivity"android:exported="true" ><intent-filter><action android:name="zxs.study.dateActivity"/><category android:name="android.intent.category.DEFAULT"/></intent-filter></activity>```
引用(用的时候记得加一个接口 :implements View.OnClickListener):```java
public void onClick(View view) {Intent intent=new Intent();switch (view.getId()){case R.id.type1:intent.setClass(this,clockActivity.class);break;case R.id.type2:intent.setAction("zxs.study.dateActivity");intent.addCategory(Intent.CATEGORY_DEFAULT);break;case R.id.type3:intent.setClassName("com.example.myapplication","com.example.myapplication.MainActivity");break;}startActivity(intent);}
startActivity(intent)不能丢。
四、总结
setp1:创建新窗口
step2:创建按钮并绑定
step3:用intent完成连接,如果是隐式需要多配置一步。
更多推荐
Android Studio 点击按钮跳转到另一个Activity
发布评论