×

android学习系列-控件用法(2)1

Kalet Kalet 发表于2013-12-20 11:07:07 浏览186 评论0

抢沙发发表评论

目录
1. ListView 
2. TableLayout 
3. LinearLayout 
4. GridView 
5. VideoView 
6. MediaProvider 
7. SqlLite 
8. ProgressBar 
9. ProgressDialog 
10. RadioButtonAndChechBox 
11. RatingBar 
12. RelativeLayout 
13. Menu

1. ListView

1.         ListView

JAVA代码

package tjuci.edu.dl;

import android.app.Activity;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.ListView;

 

publicclass ListViewSampleActivityextends Activity {

    /** Called  when the activity is first created. */

    @Override

    publicvoid onCreate(Bundle  savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        String[] str = {"aaaa","bbbbbb","cccccc","vvvvvvvv","ddd","ee"};

        ListView listView =  (ListView)findViewById(R.id.lsitview);

        ArrayAdapter<String> arrAdapter  =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,str);

        listView.setAdapter(arrAdapter);

    }

}

XML代码

<?xmlversion="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent"

     android:orientation="vertical" >

    <ListView

        android:id="@+id/lsitview"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"/>

</LinearLayout>

效果

 

 

2.TableLayout

2.        TableLayout

JAVA代码

package tjuci.edu.dl;

 

import android.app.Activity;

import android.os.Bundle;

 

publicclass RegTableActivityextends Activity {

    /** Called  when the activity is first created. */

    @Override

    publicvoid onCreate(Bundle  savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

}

XML代码

<?xmlversion="1.0" encoding="utf-8"?>

<TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"

     android:id="@+id/root"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent"

     android:orientation="vertical" >

<TableRow>

    <TextView

        android:id="@+id/tv1"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:textSize="25pt"

        android:background="@drawable/bg_border"

        android:text="用户名"/>

    <EditText

        android:id="@+id/et1"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:hint="请输入用户名"

        android:selectAllOnFocus="true"/>

</TableRow>

<TableRow>

    <TextView

        android:id="@+id/tv2"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:textSize="25pt"

        android:text="密码"/>

    <EditText

        android:id="@+id/et2"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:password="true"/>

</TableRow>

<TableRow>

    <TextView

        android:id="@+id/tv3"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:textSize="25pt"

         android:text="电话"/>

    <EditText

        android:id="@+id/et3"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:selectAllOnFocus="true"

        android:phoneNumber="true"/>   

</TableRow>

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="注册"

        />"

</TableLayout>

效果

 

 

 

3.LinearLayout

3.        LinearLayout

效果

 

JAVA代码

package tjuci.edu.dl;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

 

publicclass DataSampleextends Activity {

    /** Called  when the activity is first created. */

    @Override

    publicvoid onCreate(Bundle  savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        Button btn =  (Button)findViewById(R.id.btn1);

        btn.setOnClickListener(new  View.OnClickListener() {

            @Override

            publicvoid onClick(View v) {

                Intent intent = new Intent();

                String data = "http://www.baidu.com";

                Uri uri = Uri.parse(data);

                intent.setAction(intent.ACTION_VIEW);

                intent.setData(uri);

                startActivity(intent);

            }

        });

        Button btn2 =  (Button)findViewById(R.id.btn2);

        btn2.setOnClickListener(new  View.OnClickListener() {

            @Override

            publicvoid onClick(View v) {

                Intent intent = new Intent();

                String data = "tel:18611991643";

                Uri uri = Uri.parse(data);

                intent.setAction(intent.ACTION_DIAL);

                intent.setData(uri);

                startActivity(intent);

            }

        });

    }

}

XML代码

<?xmlversion="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent"

     android:orientation="vertical" >

 

    <Button

        android:id="@+id/btn1"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="查看www.baidu.com"/>

<Button

        android:id="@+id/btn2"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="拨打电话:13321366513"/>

</LinearLayout>

 

4.GridView

4.        GridView

效果

 

JAVA代码

package tjuci.edu.dl;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.GridView;

import android.widget.TextView;

 

publicclass XmlValuesSampleActivityextends Activity {

    //定义颜色中的文字

    private String[]words;

    /** Called  when the activity is first created. */

    @Override

    publicvoid onCreate(Bundle  savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //注:定义数组的元素 应该在onCreate方法内进行  不能再外面

        //根据Activity的生命周期可知:onCreate方法在创造Activity被调用

        words = XmlValuesSampleActivity.this.getResources().getStringArray(R.array.words);

        BaseAdapter ba = new BaseAdapter() {

            @Override

            public View getView(int position, View  convertView, ViewGroup parent) {

                //TODO Auto-generated method stub

                TextView tv = new  TextView(XmlValuesSampleActivity.this);

                tv.setWidth((int)  XmlValuesSampleActivity.this.getResources().getDimension(R.dimen.cell_width));

                tv.setHeight((int)XmlValuesSampleActivity.this.getResources().getDimension(R.dimen.cell_height));

                tv.setText(words[position]);

                tv.setBackgroundDrawable((XmlValuesSampleActivity.this.getResources().obtainTypedArray(R.array.colors)).getDrawable(position));

                tv.setTextSize(20);

                return tv;

            }

           

            @Override

            publiclong getItemId(int position) {

                //TODO Auto-generated method stub

                return position;

            }

           

            @Override

            public Object getItem(int position) {

                //TODO Auto-generated method stub

                returnwords[position];

            }

           

            @Override

            publicint getCount() {

                //TODO Auto-generated method stub

                returnwords.length;

            }

        };

        GridView gv =  (GridView)findViewById(R.id.gView);

        gv.setAdapter(ba);

    }

}

XML代码

<?xmlversion="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent"

     android:orientation="vertical" >

 

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/title" 

        android:textSize="@dimen/font_size"/>

   

    <GridView

        android:id="@+id/gView"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:horizontalSpacing="@dimen/spacing"

        android:verticalSpacing="@dimen/spacing"

        android:numColumns="3"

        android:gravity="center"

        />

</LinearLayout>

 

5.VideoView

5.        VideoView

效果

 

JAVA代码

package tjuci.edu.dl;

 

import java.io.File;

 

import android.app.Activity;

importandroid.graphics.PixelFormat;

import android.os.Bundle;

import android.widget.MediaController;

import android.widget.VideoView;

 

/**

 * Description:

 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>

 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee

 * <br/>This program is protected by  copyright laws.

 * <br/>Program Name:

 * <br/>Date:

 @author  Yeeku.H.Lee kongyeeku@163.com

 @version  1.0

 */

publicclass VedioViewTestextends Activity

{

    VideoView videoView;

    MediaController mController;

    @Override

    publicvoid onCreate(Bundle  savedInstanceState)

    {

        super.onCreate(savedInstanceState);

//      getWindow().setFormat(PixelFormat.TRANSLUCENT);

        setContentView(R.layout.main);

        //获取界面上VideoView组件

        videoView = (VideoView) findViewById(R.id.video);

        //创建MediaController对象

        mController =new MediaController(this);

        File video = new File("/mnt/sdcard/test.mp4");

        if(video.exists())

        {

            videoView.setVideoPath(video.getAbsolutePath());

            //设置videoViewmController建立关联

            videoView.setMediaController(mController);

            //设置mControllervideoView建立关联

            mController.setMediaPlayer(videoView);

            //VideoView获取焦点

            videoView.requestFocus();

        }

    }

}

XML代码

<?xmlversion="1.0" encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent"

     android:orientation="vertical" >

 

  <!--定义VideoView播放视频 -->

<VideoView

    android:id="@+id/video"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    />

</LinearLayout>

群贤毕至

访客