2019-01-22 14:34:34 weixin_44600063 阅读数 5276
  • Android移植基础

    Android视频课程,该课程可以让学员了解Android系统架构、学习如何下载Android源码、编译及开发Android、学习如何追踪Android源码、了解Linux内核启动流程、了解Android启动流程、学习如何移植外部函式库至Android源码中。

    26282 人正在学习 去看看 钟文昌

第一行代码(Android)第二版PDF和源码(分享)
最近在网上发现的《第一行代码安卓》希望能帮助到大家
已经打好包

在这呢传送门

2017-08-14 19:20:58 yyqjjw 阅读数 336
  • Android移植基础

    Android视频课程,该课程可以让学员了解Android系统架构、学习如何下载Android源码、编译及开发Android、学习如何追踪Android源码、了解Linux内核启动流程、了解Android启动流程、学习如何移植外部函式库至Android源码中。

    26282 人正在学习 去看看 钟文昌

 
1.AndroidMainfest.xml

 
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"  package="com.example.yangyq.uiwidgettest"> <application  android:allowBackup="true"  android:icon="@mipmap/ic_launcher"  android:label="@string/app_name"  android:supportsRtl="true"  android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

 


 

 

2.activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#00ff00"
        android:textSize="24sp"
        android:text="This is TextView"
        />
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:textAllCaps="false"
        />
    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入关键字"
        android:maxLines="2"
        />
    <ImageButton android:id="@+id/imgbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:src="@drawable/img_3"
        android:background="#0f0"
        android:textColor="#f00" />
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img_1"/>
    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        />
</LinearLayout>

 

 
3.MainActivity.java

 
package com.example.yangyq.uiwidgettest; import android.app.ProgressDialog; import android.content.DialogInterface; import android.preference.DialogPreference; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText editText; private ImageButton imageButton; private ImageView imageView; private ProgressBar progressBar; @Override  protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); editText = (EditText) findViewById(R.id.edit_text) ; imageView = (ImageView) findViewById(R.id.image_view); progressBar = (ProgressBar) findViewById(R.id.progress_bar); button.setOnClickListener(this); } @Override  public void onClick(View v) { switch (v.getId()){ case R.id.button: ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setTitle("This is ProgressDialog"); progressDialog.setMessage("Loading..."); progressDialog.setCancelable(true); progressDialog.show(); //弹出页面提示 // AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); // dialog.setTitle("This is Dialog"); // dialog.setMessage("Something important."); // dialog.setCancelable(false); // dialog.setPositiveButton("OK", new DialogInterface.OnClickListener(){ // @Override // public void onClick(DialogInterface dialog,int which){ // // } // }); // dialog.show();  //进度条 // int progress = progressBar.getProgress(); // progress = progress+10; // progressBar.setProgress(progress); // if(progressBar.getVisibility()== View.GONE) // { // progressBar.setVisibility(View.VISIBLE); // }else // { // progressBar.setVisibility(View.GONE); // } // imageView.setImageResource(R.drawable.img_2); // String inputText = editText.getText().toString(); // Toast.makeText(MainActivity.this,inputText,Toast.LENGTH_SHORT).show();  break; default: break; } } } 

 


 

 


 

 
2019-03-27 21:56:27 lp5800n95 阅读数 51
  • Android移植基础

    Android视频课程,该课程可以让学员了解Android系统架构、学习如何下载Android源码、编译及开发Android、学习如何追踪Android源码、了解Linux内核启动流程、了解Android启动流程、学习如何移植外部函式库至Android源码中。

    26282 人正在学习 去看看 钟文昌

#第一行代码小结

布局相关

1:定义一个id @+id/id_name
引用一个id @id/id_name
2:

    <activity-alias
            android:name="com.android.camera.CameraLauncher"
            android:icon="@mipmap/ic_launcher_camera"
            android:label="@string/snapcam_app_name"
            android:launchMode="singleTop"
            android:targetActivity="com.android.camera.CameraActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>
        
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" /> 

这两句代码,说明点击桌面应用图标时即打开这个activity
android.intent.category.DEFAULT 是默认的category,Mainfest.xml文件中自动加入

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

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

参考链接: 默认category使用

3:Intent 的使用
3.1 显示Intent
3.2 隐式Intent
3.3 向下一个活动传递数据
3.4 返回数据给上一个活动
显示Intent:
FirstActivity.java代码中:
FirstActivity.this 作为上下文,SecondActivity.class作为目标活动

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

隐式Intent:
Intent中只能指定一个action,但可以指定多个category

<category android:name="android.intent.category.DEFAULT" />    系统默认加入的

Mainfest.xml中

 <activity
        android:name=".SecondActivity"
        android:launchMode="singleInstance">
        <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>

FirstActivity.java中

Intent intent =new Intent("com.example.activitytest.ACTION_START");
intent.addCategory("com.example.activitytest.MY_CATEGORY");
startActivity(intent);

向下一个活动传递数据
FirstActivity.java中

String data="hello ";
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("key01", data1)
startActivity(intent)

SecondActivity.java中

Intent intent=getIntent();
String data=intent.getStringExtra("key01");

这样就把数据data1从FirstActivity传到了SecondActivity中

返回数据给上一个活动
FirstActivity.java

public void onClick(View v){
Intent intent =new Intent(FirstActivity.this,SecondActivity.class);
startActivityForResult(intent,1);//(Intent intent ,requestCode)
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
String returnedData = data.getStringExtra(“key01”);
Log.d(“FirstActivity”, returnedData);
}
break;
default:
}
}

SecondActivity.java

Intent intent =new Intent();
intent.putExtra(“key01”,“value01”);
setResult(RESULT_OK,intent);//(int resultCode,Intent data)
finish();

流程图如下:

FirstActivitySecondActivityStartActivityForResult()启动SecondActivityfinish()方法结束后,返回resultCode即RESULT_OKonActivityResult()被回调根据StartActivityForResult()中的请求码**requestCode**根据setResult()中的结果码**resultCode**即完成了数据从SecondActivity向FirstActivity的传递FirstActivitySecondActivity
2017-06-05 04:04:29 u010075989 阅读数 2751
  • Android移植基础

    Android视频课程,该课程可以让学员了解Android系统架构、学习如何下载Android源码、编译及开发Android、学习如何追踪Android源码、了解Linux内核启动流程、了解Android启动流程、学习如何移植外部函式库至Android源码中。

    26282 人正在学习 去看看 钟文昌
3.5最常用和最难用的控件----ListView

ListView非常常见,也是Android中最常见的空间之一,几乎所有程序都会用到,QQ聊天,刷微博,诸如此类。
ListView用法也是比较复杂。下面以一个小项目来练习ListView。
新建一个ListViewTest项目,默认设置。
public class MainActivity extends AppCompatActivity {
    private String[] data = {"Apple","Banana","Orange","Watermelon",
            "Pear","Grape","Pineapple","Strawberry","Cherry","Mango",
            "Apple","Banana","Orange","Pineapple","Strawberry","Cherry","Mango",
            "Apple","Banana","Orange"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                MainActivity.this,android.R.layout.simple_list_item_1,data);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);

    }
}
如图所示,先提供好数据,String类型的数组数据,数组中的数据无法直接传递给ListView的,要借助适配器来完成。这里用ArrayAdapter。它可通过泛型来指定要适配的实现类,然后在构造函数中把适配的数据传入。
ArrayAdapter有多个构造函数重载,这里把ArrayAdapter泛型指定为String,再在ArrayAdapter构造函数中依次传入当前上下文、ListView子项布局的id、以及要适配的数据。使用了android.R.layout.simple_list_item_1作为ListView的子项布局id,这是Android内部的布局文件,里面只有一个TextView,可用于简单显示一段文本。这样适配器对象就构建好了。
3.5.2定制ListView界面
准备好图片,对应上面提供的每一种水果。让水果名称的旁边都有一个图样。

定义一个实体类,作为List适配器的适配类型。新建Fruit类,代码如下:
public class Fruit {
    private String name;

    private int imageId;

    public Fruit(String name, int imageId) {
        this.name = name;
        this.imageId = imageId;
    }

    public String getName() {
        return name;
    }

    public int getImageId() {
        return imageId;
    }
}

然后为了ListView的子项指定一个自定义的布局,新建fruit_item.xml,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/fruit_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/fruit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"/>

</LinearLayout>

创建一个自定义适配器,继承自ArrayAdapter,将泛型指定为Fruit类。新建类FruitAdapter。

public class FruitAdapter extends ArrayAdapter {
    private int resourcedId;

    public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {

        super(context,textViewResourceId,objects);
        resourcedId = textViewResourceId;

    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView,  ViewGroup parent) {
        Fruit fruit = getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(resourcedId,parent,
                false);
        ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
        TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
        fruitImage.setImageResource(fruit.getImageId());
        fruitName.setText(fruit.getName());
        return view;
    }
}

修改MainActivity代码:

public class MainActivity extends AppCompatActivity {
   private List<Fruit> fruitList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits();
        FruitAdapter adapter = new FruitAdapter(MainActivity.this,
                R.layout.fruit_item,fruitList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);

    }

    private void initFruits() {
        for (int i = 0;i<2;i++) {
            Fruit apple = new Fruit("Apple",R.drawable.apple_pic);
            fruitList.add(apple);
            Fruit banana = new Fruit("Apple",R.drawable.banana_pic);
            fruitList.add(banana);
            Fruit orange = new Fruit("Apple",R.drawable.orange_pic);
            fruitList.add(orange);
            Fruit watermelon = new Fruit("Apple",R.drawable.watermelon_pic);
            fruitList.add(watermelon);
            Fruit pear = new Fruit("Apple",R.drawable.pear_pic);
            fruitList.add(pear);
            Fruit grape = new Fruit("Apple",R.drawable.grape_pic);
            fruitList.add(grape);
            Fruit pineapple = new Fruit("Apple",R.drawable.pineapple_pic);
            fruitList.add(pineapple);
            Fruit strawberry = new Fruit("Apple",R.drawable.strawberry_pic);
            fruitList.add(strawberry);
            Fruit cherry = new Fruit("Apple",R.drawable.cherry_pic);
            fruitList.add(cherry);
            Fruit mango = new Fruit("Apple",R.drawable.mango_pic);
            fruitList.add(mango);
        }
    }
}

如图所示

2015-04-21 18:34:37 alvinyanyang 阅读数 1866
  • Android移植基础

    Android视频课程,该课程可以让学员了解Android系统架构、学习如何下载Android源码、编译及开发Android、学习如何追踪Android源码、了解Linux内核启动流程、了解Android启动流程、学习如何移植外部函式库至Android源码中。

    26282 人正在学习 去看看 钟文昌

一 : FirstActivity.java

package com.example.activitytest;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class FirstActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.first_layout);
        Button button1 = (Button) findViewById(R.id.button_1);
        //****test1
    //  button1.setOnClickListener(new OnClickListener(){
    //      @Override
    //      public void onClick(View v){
    //          Toast.makeText(FirstActivity.this,"You clicked Button 1", Toast.LENGTH_SHORT).show();
    //  button1.setOnClickListener(new OnClickListener(){
    //      @Override
    //****显式Intent
    //      public void onClick(View v){
    //          Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
    //          startActivity(intent);
    //****隐式Intent
    //  button1.setOnClickListener(new OnClickListener(){
    //      @Override
    //      public void onClick(View v){
    //          Intent intent =new Intent("com.example.activitytest.ACTION_START");
    //      startActivity(intent);  
        button1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
    //          Intent intent =new Intent("com.example.activitytest.ACTION_START");
    //          intent.addCategory("com.example.activitytest.MY_CATEGORY");
    //          startActivity(intent);
    //***调用浏览器
    //          Intent intent =new Intent(Intent.ACTION_VIEW);
    //          intent.setData(Uri.parse("http://www.baidu.com"));
    //          startActivity(intent);
    //***调用系统拨号
    //          Intent intent =new Intent(Intent.ACTION_DIAL);
    //          intent.setData(Uri.parse("tel:10010"));
    //          startActivity(intent);
    //***向下一活动传递数据
    //          String data ="Hello SecondActivity";
    //          Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
    //          intent.putExtra("extra_data", data);
    //          startActivity(intent);
    //***返回数据给上一个活动
                Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
                startActivityForResult(intent,1);           
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        switch(requestCode){
        case 1:
            if (resultCode==RESULT_OK){
                String returnedData =data.getStringExtra("data_return");
                Log.d("FirstActivity",returnedData);
            }
            break;
            default:
        }
    }
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
    case R.id.add_item:
        Toast.makeText(this, "You clicked Add", Toast.LENGTH_SHORT).show();
        break;
    case R.id.remove_item:
        Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show();
        break;
        default:
    }
    return true;
    }
}

二 : SecondActivity.java

package com.example.activitytest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class SecondActivity extends Activity{
    @Override
    protected void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.second_layout);
    //***向下一活动传递数据
    //  Intent intent =getIntent();
    //  String data = intent.getStringExtra("extra_data");
    //  Log.d("SecondActivity",data);
    //***返回数据给上一个活动
        Button button2 =(Button) findViewById(R.id.button_2);
        button2.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent = new Intent();
                intent.putExtra("data_return","Hello FirstActivity");
                setResult(RESULT_OK,intent);
                finish();
            }
        });
        }
    @Override
    public void onBackPressed(){
        Intent intent =new Intent();
        intent.putExtra("data_return", "Hello FirstActivity");
        setResult(RESULT_OK, intent);
        finish();
    }

}

三 : ThirdActivity.java

package com.example.activitytest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class ThirdActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.third_layout);

}
}

四 : BaseActivity

package com.example.activitytest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class BaseActivity extends Activity{
    @Override
    protected void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);
        Log.d("BaseActivity",getClass().getSimpleName());
        ActivityCollector.addActivity(this);
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
}

五 :ActivityCollector

package com.example.activitytest;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;

public class ActivityCollector {
    public static List<Activity> activities = new ArrayList<Activity>();
    public static void addActivity(Activity activity){
        activities.add(activity);
    }
    public static void removeActivity(Activity activity){
        activities.remove(activity);
    }
    public static void finishAll(){
        for(Activity activity:activities){
            if (!activity.isFinishing()){
                activity.finish();
            }
        }
    }
}

六 : AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activitytest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity 
            android:name=".FirstActivity"
            android:label="This is my first activity">
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            <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>
        <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>
    </application>

</manifest>

七 : first_layout.xml

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

八: second_layout.xml

<?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_2"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="Button 2"/> 
</LinearLayout>

九 : third_layout.xml

<?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_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 3"
        />

</LinearLayout>

menu/main.xml

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

完。

没有更多推荐了,返回首页