Android中Activity相关知识-1

  • 创建Activity
    • Activity是四大组件之一,一个App可以有0个或多个Activity
    • 它是一个用来展示用户界面以及与用户进行交互操作的组件
    • 当在一个Activity中按下返回时,Android OS会自动销毁Activity对象
    • 当在一个Activity中启动另外一个Activity时,当前Activity不会被销毁,而是会被压入Activity栈中
    • Activity跳转的其他Activity可以不是当前Application中的Activity
  • Activity的创建步骤:
    • 自定义类,继承Activity 
    • 复写onCreate方法,并使用setContentView方法设置显示内容 
    • 在AndroidManifest.xml文件中注册Activity
注意: 以下方式来指定APP默认启动的
   
   
<intent-filter>
<action android:name="android.intent.action.MAIN" />
 
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
  • Intent意图(四大组件的粘合剂)
    • 显示Intent:
      • 创建Intent时,已经明确需要启动哪一个具体的Activity   
    
    
Intent intent = new Intent (Context context, Class destination);
startActivity(intent);
Intent intent=new Intent();
intent.setClassName("com.escott.day06_1","com.escott.day06_1.BActivity");
  • 隐式Intent:
    • 创建的Intent,不知道具体是哪一个Activity,由Android OS根据action和category进行过滤查找    
   
   
Intent intent = new Intent (“action_danny_jiang”);
startActivity(intent);
  • 实现跳转的基本步骤:
    • 首先创建工程文件
    • 在xml文件中定义一个按钮为了实现后面点击跳转
    • 在.java文件中利用拖拽的方式快捷生成另一个.java文件(创建跳转后的activity)
    • 同时生成另一个.xml文件在其中创建一个TextView显示在跳转后的页面上
    • 在MainActivity.java文件中对按钮进行实现初始化以及点击跳转动作以及页面的跳转
    • 注意:不要忘记在注册文件中对新的.java文件进行注册(代码如下)
一、
     
     
<!--在xml文件中创建button-->
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="启动BActivity" />
二、
     
     
<!--创建另一个.java文件并继承于Activity-->
public class BActivity extends Activity {
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
}
}
三、
    
    
<!--创建.xml文件同时创建一个TextView(创建什么随意)-->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="this is BActivity" />
四、
    
    
public class MainActivity extends Activity {
private Button button1=null;
//创建私有静态变量
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<!--初始化按钮事件-->
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//创建Intent
Intent intent=new Intent(MainActivity.this,BActivity.class);
//调用Context.startActivity进行页面跳转
startActivity(intent);
}
});
}
}
另一种方法:
    
    
Intent intent=new Intent();
intent.setClassName("com.escott.day06_1","com.escott.day06_1.BActivity");
//注意后面的类名要是全路径才行
如果再次创建一个CActivity.java同时创建一个xml文件在layout中,注册代码如下:
    
    
<activity android:name="com.escott.day06_1.DActivity">
<intent-filter>
<action android:name="tiao_zhuan"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
效果如下图:




  • Application的传参方法:
    • 首先创建一个.Java文件名为MyApplication
    • 在注册文件中进行注册
      
      
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
<!--进行注册-->
android:name="com.scott.day06_1.MyApplication"
android:theme="@style/AppTheme"
  • 在两个.Java文件中写上标记(查看执行顺序)
        
        
Toast.makeText(MyApplication.this, "启动MyApplication", Toast.LENGTH_SHORT).show();
  • 执行打印先后是先出现MyApplication后出现MainApplication
  • 在MyApplication中定义一个静态的score变量,同时创建方法返回score值
          
          
private int score=100;
@Override
public void onCreate() {
super.onCreate();
}
public int getScore(){
return score;
}
  • 在MainApplication中初始化创建MyApplication的对象调用在MyApplication中创建的方法返回score的值同时显现出来
            
            
注意无论后面创建几个.Java文件只要是通过getApplication()获取的MyApplication(是一个单例)对象都是指同一个对象
MyApplication app=(MyApplication) getApplication();
int score=app.getScore();
Toast.makeText(MainActivity.this, "score="+score, Toast.LENGTH_SHORT).show();
  • 简单说来就是在MyApplication中定义方法后再其他的.Java文件中调用操作,不要忘记注册
注意:
          setText()方法:一种是直接传入参数是字符串,一种是传输参数是整型; 如果是字符串,则直接赋值到setTex中,如果是传入参数是整型,则会去 resource中根据整型查找对应的字符串,然后转化为字符串。
  • 使用intent.putExtra(String,String)传递intent的值
  • getIntent是返回启动此Activity的Intent
  • 从Intent中获取源Activity中put的相应值
    
    
String name=intent.getStringExtra("name");

  • Activity生命周期
    • onCreate    创建Activity时被调用
      onRestart    Activity被重新显示时被调用
      onStart    显示Activity时被调用
      onResume    Activity获取焦点时被调用(用户可以与Activity交互)
      onPause    Activity失去焦点时被调用
      onStop    Activity不可见时被调用
      onDestroy    Activity销毁时被调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值