第一行代码的笔记
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//不显示标题栏,要在OnCreate之前执行
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.first_layout);
//使用吐司
Button button1 = (Button) findViewById(R.id.button_1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(FirstActivity.this, "点击啦", Toast.LENGTH_LONG).show();
}
});
}
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity android:name=".FirstActivity" android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<Button android:id="@+id/button_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button_1" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/add_item" android:title="Add" />
<item android:id="@+id/remove_item" android:title="Remove" />
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.main, menu);
//false无法显示
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.add_item:
Toast.makeText(this, "点了加", Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this, "点了清除", Toast.LENGTH_SHORT).show();
break;
default:
}
return true;
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// Toast.makeText(FirstActivity.this, "点击啦", Toast.LENGTH_LONG).show();
finish();
}
新键一个layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<Button android:id="@+id/button_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" />
</LinearLayout>
创建Activity:
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second_layout);
}
}
<activity android:name=".SecondActivity" >
</activity>
设置点击事件:
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
<activity android:name=".SecondActivity" >
<intent-filter >
<action android:name="com.example.activitytest.ACTION_START"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.activitytest.MY_CATEGORY" />"
</intent-filter>
</activity>
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//intent只能指定一个action,但能指定多个category
Intent intent = new Intent("com.example.activitytest.ACTION_START");
intent.addCategory("com.example.activitytest.MY_CATEGORY");
startActivity(intent);
}
使用内置浏览器:
public void onClick(View v) {
// TODO Auto-generated method stub
//安卓内置动作
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
Intent-filter的data标签:
android:scheme //指定协议
android:host //指定主机名
android:port //指定端口
android:path //主机名和端口之后的部分
android:mimeType //指定可以处理的数据类型
只有data中指定的内容和intent中携带的data完全一致时,当前活动才能够响应改intent
自建一个活动能响应浏览器:
<activity android:name=".ThirdActivity" >
<intent-filter >
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
//能响应上面的访问网站的请求
<data android:scheme="http"/>
</intent-filter>
</activity>
调用拨号:
public void onClick(View v) {
// TODO Auto-generated method stub
//拨号
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:100086"));
startActivity(intent);
}
String data = "哈哈";
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("info", data);
startActivity(intent);
Intent intent = getIntent();
String data = intent.getStringExtra("info");
Log.d("SecondeActivity", data);
FirstActivity:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
//第二个参数是请求码,用于回调中判断数据的来源,请求码是唯一值
startActivityForResult(intent, 1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch(requestCode){
//确定来源
case 1:
if(resultCode == RESULT_OK){
String returnedData = data.getStringExtra("data_return");
Log.d("FirstAcitivity", returnedData);
}
break;
default:
}
}
SecondActivity:
Button button2 = (Button) findViewById(R.id.button_2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("data_return", "Hello First");
//第一个参数是返回的结果
setResult(RESULT_OK,intent);
finish();
}
});
如果不想按button键返回,按返回键返回则要重写:
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("data_return", "hello firstacitivity");
setResult(RESULT_OK,intent);
finish();
}