Android Studio 点击按钮跳转到另一个Activity

编程入门 行业动态 更新时间:2024-10-05 21:17:05

Android Studio 点击按钮<a href=https://www.elefans.com/category/jswz/34/1768064.html style=跳转到另一个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

本文发布于:2024-02-07 04:03:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1752725.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:跳转到   按钮   Android   Studio   Activity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!