一、安卓上手(Intent)

第一行代码的笔记

入门

Activity的基本写法:


@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();
        }
    });
}

AndroidManifest.xml基本写法

   <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>

Layout基本写法

<?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>

使用Menu

<?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();
}

使用Intent

显式intent

新键一个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);
}

隐式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);
}

更多隐式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();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值