Android 开发基础

发布于 2016-07-01  1,314 次阅读


Activity 控件基本元素

id="@+id/id"

layout_width="match_parent" //和父元素一样

layout_height="warp_content"//紧凑的 刚好包裹子内容

text="text"

 

创建Activity

class extenda Activity

@Override

OnCreate{

super.onCreate(saveInstanceState);

setContentViw(R.layout.XXX);

}

<activity

name=".classname"

label="activityname">

<intent-filter>

<action name="android.intent.action.MAIN" /> ? //可以通过这个activity作为入口打开app

<category?name="android.intent.category.LAUNCHER" /> //表示在启动器中显示这个activity

</intent-filter>

</activity>

 

显示Toast

Toast.makeText(content,Text,Toast.TYPE).show();

 

销毁活动退出APP

finish(); (等价于按下back按键)

System.exit(0);

 

显式Intent

Intent intent=new Intent(thisclassname.class,otherclassname.class);

startActivity(intent);

 

隐式Intent

Intent intent =newIntent("com.example.activitytest.ACTIONSTART");

intent.addCategory("android.intent.category.MY_CAEGORY")

startActivity(intent);

 

<activity?>

<intent-filter>

<action name="com.example.activitytest.ACTIONSTART" /> ? //自定义的action

<category?name="android.intent.category.MY_CAEGORY" />

<category?name="android.intent.category.DEFAULT" />

</intent-filter>

</activity?>

 

 

常用的Intent

Intent intent=newIntent(Intent.ACTION.ACTION_VIEW);

intent.sdtData(Uro.parse("http://www.baidu.com"));

startActivity(intent);

 

Intent intent=newIntent(Intent.ACTION.ACTION_DIAL);

intent.sdtData(Uro.parse("tel:10086"));

startActivity(intent);

 

Intent 传递数据

向下传递

valuedata="hello world";

Intent intent=new Intent(this.class,otherclass.class);

intent.putExtra("Key",value);

startActivity(intent);

读取数据

onCreate{
Intent intent=getIntent();

String value=intent.getStringExtar("key");

}

返回数据到上一个活动

Intent intent=new Intent(this.class,other.class);

startActivityForResult(intent,1);

---->

 

override onPressBack()

Intent intent=new Intent();

intent.putExtra("key",value);

SetResult(RESULT_OK,intent);

finish();

 

<----

活动的状态

运行状态 ?暂停状态 (不位于栈顶 但仍可见) 停滞状态 销毁状态

活动的生命周期

onCreate 第一次创建

onStart 由不见转为可见的时候

onResume 处于活动状态的时候

onPause 准备去启动或者恢复另一个活动的时候

onStop 活动完全不可见的时候

onDestroy 销毁之前

onRestart 又停止状态滨城运行状态的时候

 

onSaveInstanceState

@Override

protected?void?onSaveInstanceState(Bundle?outState)?{

//?TODO?Auto-generated?method?stub

outState.putInt("currentposition",?videoView.getCurrentPosition());

Log.v("tag",?"onSaveInstanceState");

super.onSaveInstanceState(outState);

}

if?(savedInstanceState?!=?null

&&?savedInstanceState.getInt("currentposition")?!=?0)?{

videoView.seekTo(savedInstanceState.getInt("currentposition"));

}

 

活动的启动模式

<activity ?launcherfMode="standard"/>

standard (默认)

每次启动都是该活动的新实例

singleTop

如果需要新创建的活动已经在栈顶了 ?那么不会创建新的实例

singleTask

检查需要创建的活动 是否子活动栈中存在实例 有则直接调用

 

控件的基本属性

id="@+id/id"

layout_width="" //?match_parent fill_parent warp_content

layout_height="" //match_parent fill_parent warp_content

text="text"

gravity="center" //对齐方式 可选值 ?top bottom left right center ?多个值用|分隔

textSize="24sp"

textColor="#fff"

hint="请输入密码" //提示文字 ?和html的placeholder属性一样

maxLines="2"

visibility="" //可选 visible(可见) invisible(不可见 但依旧占用空间) gone (不可见 不占用空间) 方法 ?getVisibility()==View.GONE

onclick事件绑定

方法1

Button button=(Button)findViewById(R.id.myid);

button.setOnClickListenrer(new onClickLinster(){

@override

public void onClick(View v){

}

})

方法2

mainActivity extends Activity implements onClickLinstener{

onCreate(){

Bottn btn=(button)findViewById(R.id.myid);

btn.setOnClickLinstener(this);

}

@override

obClick(View v){

switch(v.getID()){

case R.id.myid:

break;

default"

break;
}

}

}

ProgressBar

进度条控件

style="?android:attr/progressbarstyleHorizontal" //默认是转动的圈圈样式 ?现在改成水平的进度条

修改进度:

Int progress=progressBAr.getProgress();

progress+=10;

progressbar.setprogress(progress);

AlertDialog

AlertDialog.Builder dialog=new AlterDialog.Builder(mainActivity.class);

dialog.setTitle("");

dialog.setMessage("");

dialog.setCancelable(false);

dialog.serPositiveButton("OK",New DialogInterface.OnclickListener(){

@override

onclick(){

}

})

dialog.serNegativeButton("cancel",new DialogInterface.OnClickLinstener(){

@override

onclick(){

}

})

dialog.show();

ProgressDialog

ProgressDialog progreedialog=new progressDialog(MainActivity.this);

progreedialog.setTitle();

progreedialog.SetMEssage();

progreedialog.setcanceable(true);

progreedialog.show();

progreedialog.dismiss();

 

 

四大基本布局

LinerLayout线性布局

orientation="vertival" //垂直方向上的线性布局

orientation="horizontal" // 水平方向上的线性布局

layout_gravity ?//对齐方式

layout_weight ?控件水平所占宽度

 

RelativeLayout

相对布局

layout_alignParentRight ="true"

layout_centerinparent="true"

layout_above="id"

layut_toLeftof="id"

 

FrameLayout

 

tablelayout

表格布局

 

 

 

届ける言葉を今は育ててる
最后更新于 2017-10-13