Android-1
@String 支持多语言
layout中的text文本中String都尽量定义在String.xml中,便于多语言管理。
<resources>
<string name="app_name">HelloWorld</string>
<string name="action_settings">Settings</string>
<string name="text1">hello,you are the best!</string>
<string name="btn1">Jump to Another Activity</string>
<string name="title_activity_another">AnotherActivity</string>
</resources>
跳转另一个actvity
①监控点击动作可以有两种方式:
1> 在layout中设置Button onclick属性为方法名,在activity里编写方法
2> 在activity里findViewbyID 找到Button控件 ,设置监听事件
如采取第一种方法,要注意:
note that, in order for the system to match this method to the method name given to android:onClick
, the signature must be exactly as shown. Specifically, the method must:
- Be public
- Have a void return value
- Have a
View
as the only parameter (this will be theView
that was clicked)
android:onClick="send"
public void send(View view){
Intent intent = new Intent(this,AnotherActivity.class);
startActivity(intent);
}
跳转网页
public void sendToBrowser(View view){
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com")));
}
activity的生命周期&activity跳转时的生命周期
通过在代码中activity每个生命周期中添加system.out,了解activity生命周期
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("A onCreate");
}
protected void onStart() {
super.onStart();
System.out.println("A onStart");
}
protected void onResume() {
super.onResume();
System.out.println("A onResume");
}
..
..
..
在两个activity跳转的过程中,上一个activity如果不可见,则先执行onPause(),再执行onStop()
如果该activity仍可见(比如下一个activity是dialogue,则只执行onPause(),关闭dialogue直接执行onResume())
将activity设为dialogue的方法,在manifest文件中修改:
<activity
android:name=".AnotherActivity"
android:label="@string/title_activity_another"
android:theme="@style/Base.Theme.AppCompat.Dialog">
</activity>