package example.jay.com.activitytest1; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; public class MyActivity extends ActionBarActivity { private Button btnregister; private EditText editname; private RadioGroup rad; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); btnregister = (Button)findViewById(R.id.btnregister); editname = (EditText)findViewById(R.id.editname); rad = (RadioGroup)findViewById(R.id.radioGroup); btnregister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name,sex = ""; Intent it = new Intent(MyActivity.this,MyActivity2.class); name = editname.getText().toString(); //遍历RadioGroup找出被选中的单选按钮 for(int i = 0;i < rad.getChildCount();i++) { RadioButton rd = (RadioButton)rad.getChildAt(i); if(rd.isChecked()) { sex = rd.getText().toString(); break; } } //新建Bundle对象,并把数据写入 Bundle bd = new Bundle(); bd.putCharSequence("user",name); bd.putCharSequence("sex",sex); //将数据包Bundle绑定到Intent上 it.putExtras(bd); startActivity(it); //关闭第一个Activity finish(); } }); } }
package example.jay.com.activitytest1; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.widget.TextView; public class MyActivity2 extends ActionBarActivity { private TextView txtshow; private String name; private String sex; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_activity2); txtshow = (TextView)findViewById(R.id.txtshow); //获得Intent对象,并且用Bundle出去里面的数据 Intent it = getIntent(); Bundle bd = it.getExtras(); //按键值的方式取出Bundle中的数据 name = bd.getCharSequence("user").toString(); sex = bd.getCharSequence("sex").toString(); txtshow.setText("尊敬的"+name + " " + sex + "士"+"恭喜你,注册成功~"); } }
2.多个Activity间的交互(后一个传回给前一个)
3.知晓当前是哪个Activity
4.随时关闭所有Activity
具体代码如下: public class ActivityCollector { public static LinkedList<Activity> activities = new LinkedList<Activity>(); public static void addActivity(Activity activity) { activities.add(activity); } public static void removeActivity(Activity activity) { activities.remove(activity); } public static void finishAll() { for(Activity activity:activities) { if(!activity.isFinishing()) { activity.finish(); } } } }
5.完全退出App的方法 上面说的是关闭所有Activity的,但是有些时候我们可能想杀死整个App,连后台任务都杀死 杀得一干二净的话,可以使用搭配着下述代码使用:
实现代码: /** * 退出应用程序 */ public void AppExit(Context context) { try { ActivityCollector.finishAll(); ActivityManager activityMgr = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); activityMgr.killBackgroundProcesses(context.getPackageName()); System.exit(0); } catch (Exception ignored) {} }
6.双击退出程序的两种方法: 1)定义一个变量,来标识是否退出 // 定义一个变量,来标识是否退出 private static boolean isExit = false; Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); isExit = false; } }; public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (!isExit) { isExit = true; Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show(); // 利用handler延迟发送更改状态信息 mHandler.sendEmptyMessageDelayed(0, 2000); } else { exit(this); } return false; } return super.onKeyDown(keyCode, event);} 2)保存点击时间: //保存点击的时间 private long exitTime = 0; public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if ((System.currentTimeMillis() - exitTime) > 2000) { Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show(); exitTime = System.currentTimeMillis(); } else { exit(); } return false; } return super.onKeyDown(keyCode, event); }
2)方法二: 通过style进行配置,这个是全局的哦,就是所有的Activity都会加载这个动画! 实现代码如下: ①在style.xml中自定义style: <!-- 默认Activity跳转动画 --> <style name="default_animation" mce_bogus="1" parent="@android:style/Animation.Activity"> <item name="android:activityOpenEnterAnimation">@anim/default_anim_in</item> <item name="android:activityOpenExitAnimation">@anim/anim_stay</item> <item name="android:activityCloseEnterAnimation">@anim/anim_stay</item> <item name="android:activityCloseExitAnimation">@anim/default_anim_out</item> </style> 解释: 4个item分别代表: Activity A跳转到Activity B时Activity B进入动画; Activity A跳转到Activity B时Activity A退出动画; Activity B返回Activity A时Activity A的进入动画 Activity B返回Activity A时ActivityB的退出动画 ②然后修改下AppTheme: <style name="AppTheme" mce_bogus="1" parent="@android:style/Theme.Light"> <item name="android:windowAnimationStyle">@style/default_animation</item> <item name="android:windowNoTitle">true</item> </style> ③最后在appliction设置下: <application android:icon="@drawable/logo" android:label="@string/app_name" android:theme="@style/AppTheme" >
10.设置Activity全屏的方法: 1)代码隐藏ActionBar 在Activity的onCreate方法中调用getActionBar.hide();即可 2)通过requestWindowFeature设置 requestWindowFeature(Window.FEATURE_NO_TITLE); 该代码需要在setContentView ()之前调用,不然会报错
3)通过AndroidManifest.xml的theme 在需要全屏的Activity的标签内设置 theme = @android:style/Theme.NoTitleBar.FullScreen 11.onWindowFocusChanged方法妙用: 我们先来看下官方对这个方法的介绍:
12.定义对话框风格的Activity 直接设置下Activity的theme: android:theme="@android:style/Theme.Dialog"这样就可以了,当然你可以再设置下标题,小图标! //设置左上角小图标 requestWindowFeature(Window.FEATURE_LEFT_ICON); setContentView(R.layout.main); getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.ic_lion_icon); //设置文字: setTitle(R.string.actdialog_title); //XML代码中设置:android:label="@string/activity_dialog"