zoukankan      html  css  js  c++  java
  • Android Studio 学习(一)

    XML给元素定义ID

    • 定义id android:id="@+id/button_1"
    • 引用id id/id_name

    解决Failed to load Appcompat Actionbar with unknown error

    • 在Project 中找到appsrcmain esvaluesstyles.xml
    • 更改<style name="AppTheme" parent="Theme.Appcompat.Light.DarkActionBar">
      <style name="AppTheme" parent="Base.Theme.Appcompat.Light.DarkActionBar">

    在AndroidManifest.xml文件注册活动为主活动

    <activity android:name=".FirstActivity" android:label="This is FirstActivity"//项目的标题> <intent -filter> <action android:name="android.intent.action.Main"/>//主活动 <category android:name="android.intent.category.LAUNCHER"/>//首先启动这个活动 <i/intent-filter> </activity>

    在布局中添加按钮

    <Button android:id="@+id/button_1"//代码中指定的名字 android:layout_width="match_parent" android:layout_height="wrap_content"//高度随内容而变 android:text="Button1" />

    在主活动上 配置按钮的点击事件

    Button button1=(Button) findViewById(R.id.button_1); button1.setOnClickListener(new View.OnClickListener()) { @Override public void onClick(View v) { } }

    Toast 小提醒

    Toast.makeText(FirstActivity.this,"text",Toast.LENGTH_SHORT/LENGTH_LONG).show();

    菜单

    `res->menu->main(Menu resource file)

    //菜单选项名字 `

    ctrl+o 重写onCreateOptionsMenu() 方法

    public boolean onCreateOptionsMenu (Menu menu) { getMenuInflater().inflate(R.menu.main,menu); return true; }//显示菜单

    重写onOptinsItemSelected 方法
    public boolean onOptionsItemSeleted { swith (item.getItemId()) { switch(item.getItemId()) { case R.id.add_item: Toast.makeText(this,"you clicked add",Toast.LENGTH_SHORT).show(); break; } return true; } }

    摧毁一个活动

    finish();

    使用intent切换活动

    • 显式切换
      Intent intent =new Intent(FirstActivity.this ,SecondActivity.class); startActivity(intent);

    • 隐式切换

    • Intent intent =new Intent ("com.example.activitytest.ACTION_START");

    • intent.addCategory("com.example.activitytest.MY_CATEGORY");

    • stratActivity(intent);//默认category
      <intent -filter>


    向下一个活动传递数据

    • Intent intent=new Intent(FirstActivity.this ,SecondActivity.class);

    • intent.putExtra("extra_data",data)

    • Intent intent=getIntent();

    • String data=intent.getStringExtra('extar_data');

    返回数据给上一个活动

    在第一个活动中

    • Intent intent =new Intent (FirstActivity.this,SecondActivity.class);

    • startActivityForResult(intent,1)//1为请求码

    • switch(requestCode){//重写onActivityResult方法
      case 1:
      if(resultCode==RESULT_OK)
      {
      String returnedData=data.getStringExtra("data_return");
      }
      }

    在第二个活动中

    按钮退出

    • Intent intent=new intent();
    • intent.putExtra("data_return","Hello Fiffff..")
    • setResult(RESULT_OK,intent)//对应上面的forresult方法 第一个参数两个值 RESULT_CANCELED
    • finash();‘

    back退出 重写onBackPressde()方法
    内容与上面一样

    按钮排列bug

    • The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with designtime attributes (such as layout_editor_absoluteX.) These attributes are not applied at runtime。
    • 点击这个 infer constraints

    活动回收栈 保存临时数据

    • 重写onSaveInstanceState()方法

    • outState.putString("data_key",tempData);

    • 在onCreate方法中有

    • if (savedInstanceState!=null)
      {
      String tempData=savedInstanceState.getString("data_key");
      }

    活动的启动模式

    • 四种 standard singleTop singleTask singleInstance
    • 在mainfest.xml中注册 android:launchMode="singleTop"
    • standard 每次启动都会创建一个新的活动
    • singleTop 在栈顶时创建不会创建新的 而是直接用当前的 不在栈顶时创建新的
    • singleTask 有活动时 这个活动之上的活动都出栈 没有活动时直接创建
    • singleInstance 这个活动会创建新的栈

    启动活动的最佳写法

    p74

  • 相关阅读:
    属性的自动完成
    二十七、详测 Generics Collections TDictionary(3): TPairEnumerator、TKeyEnumerator、TValueEnumerator、ExtractPair
    Delphi 反转内存的函数
    类方法调用
    九、泛型排序器 TComparer
    十九、详测 Generics Collections TList (10): OnNotify 事件
    二十、详测 Generics Collections TList (11): Create
    二十一、详测 Generics Collections TQueue (1): Enqueue、Dequeue、Peek
    十四、详测 Generics Collections TList (5): Move、Exchange
    二十二、详测 Generics Collections TQueue (2): Create、Count、Clear、TrimExcess
  • 原文地址:https://www.cnblogs.com/lancelee98/p/9358943.html
Copyright © 2011-2022 走看看