安卓第八天——四个UI常用组件

 

安卓第八天

UI组建

以下是四个组建的具体实现

RadioGroup RadioButton

 

  <RadioGroup

        android:id="@+id/sexRg"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:checkedButton="@+id/woman"

        android:orientation="vertical" >

 

        <RadioButton

            android:id="@id/woman"

            android:text="女" />

 

        <RadioButton

            android:id="@+id/man"

            android:text="男" />

    </RadioGroup>

 

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Button" />

 

 

UItestActivity3Activity

rg = (RadioGroup) findViewById(R.id.sexRg);

 

rg.setOnCheckedChangeListener(this);

 

public void onCheckedChanged(RadioGroup group, int checkedId) {

    if (group.equals(rg)) {

       RadioButton rb = (RadioButton) this.findViewById(checkedId);

       Toast.makeText(this, rb.getText(), Toast.LENGTH_SHORT).show();

    }

}

//-------------

 

Button btn1 = (Button) this.findViewById(R.id.button1);

btn1.setOnClickListener(new OnClickListener() {

 

 

 

CheckBox

 

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

 

   

       

      <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="爱好" />

   <TableLayout

      

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

       android:stretchColumns="*"

       android:id="@+id/tablelayout"

       >

      

       <TableRow >

        <CheckBox

       android:id="@+id/checkBox1"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="CheckBox" />

       

         <CheckBox

       android:id="@+id/checkBox2"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="CheckBox2" />

       </TableRow>

      

      

       <TableRow >

        <CheckBox

       android:id="@+id/checkBox3"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="CheckBox3" />

       

         <CheckBox

       android:id="@+id/checkBox4"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="CheckBox4" />

       </TableRow>

       

      

      

      

      

   </TableLayout>

 

   <Button

       android:id="@+id/submit"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="提交" />

 

</LinearLayout>

 

 

 

 

 

package cn.class3g.activity;

 

import java.util.ArrayList;

 

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

 

public class CheckBoxDemo extends Activity implements OnCheckedChangeListener {

 

    private static final String TAG="TAG";

   private  CheckBox  cb1,cb2,cb3,cb4;

    private Button submit;

   private ArrayList<CheckBox> list= new ArrayList<CheckBox>();

  

      public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.checkbox_layout);

           

           

            findviews();

      }

 

    private void findviews() {

        // TODO Auto-generated method stub

        cb1=(CheckBox) this.findViewById(R.id.checkBox1);

        cb2=(CheckBox) this.findViewById(R.id.checkBox2);

        cb3=(CheckBox) this.findViewById(R.id.checkBox3);

        cb4=(CheckBox) this.findViewById(R.id.checkBox4);

   

        list.add(cb1);

        list.add(cb2);

        list.add(cb3);

        list.add(cb4);

       

        for(CheckBox cb:list){

           

            cb.setOnCheckedChangeListener(this);

           

        }

       

        submit=(Button) this.findViewById(R.id.submit);

       

        submit.setOnClickListener( new OnClickListener() {

           

       

            public void onClick(View v) {

           

                String fav="";

                for(CheckBox cb:list){

                   

                    if(cb.isChecked()){

                       

                        fav+= cb.getText()+",";

                    }

                }

           

                Log.i(TAG, fav);

            }

        });

           

   

   

       

    }

 

    @Override

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        // TODO Auto-generated method stub

        Log.i(TAG, buttonView.getText().toString());

       

    }

}

ListView

 

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

 

    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="名单" />

 

   

    <ListView

       

         android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/nameList"

      

        />

</LinearLayout>

 

ListViewDemo

package cn.class3g.activity;

 

import java.util.ArrayList;

 

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;

import android.widget.ListView;

 

public class ListViewDemo extends Activity implements OnItemClickListener{

    private static final String TAG="TAG";

    ListView nameList=null;

    String [] names={"张三","李四","王五","赵六"};

     public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.list_layout);

           

            findViews();

           

     }

 

    private void findViews() {

       // TODO Auto-generated method stub

      

       nameList=(ListView) this.findViewById(R.id.nameList);

      

       ArrayAdapter adapter =new ArrayAdapter(this,

              android.R.layout.simple_list_item_1,names);

       nameList.setAdapter(adapter);

       nameList.setOnItemClickListener(this);

    }

 

   

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

       // TODO Auto-generated method stub

      

       Log.i(TAG, names[arg2]+"Possion="+String.valueOf(arg2)+"row_id="+String.valueOf(arg3));

      

    }

}

 

 

 

Spinner

 

<?xml version="1.0" encoding="utf-8"?>

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

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:orientation="vertical" >

 

    <TextView

       

        android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="请选择一项运动项目"

       

       

        />

   

   

    <Spinner

       

        android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:id="@+id/sportsSp"

      android:prompt="@string/spiner"

        android:entries="@array/sports"

     

        />

 

</LinearLayout>

 

package cn.class3g.activity;

 

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.Spinner;

import android.widget.TextView;

 

public class SpinerDemo extends Activity implements OnItemSelectedListener {

 

    private static final String TAG="TAG";

    Spinner sportSp=null;

     public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.spiner_layout);

           

           

            findViews();

        }

    private void findViews() {

       // TODO Auto-generated method stub

      

       sportSp=(Spinner) this.findViewById(R.id.sportsSp);

       sportSp.setOnItemSelectedListener(this);

      

    }

   

    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,

           long arg3) {

   

    TextView tv=  (TextView) arg1;

   

    Log.i(TAG, tv.getText().toString());

    }

   

    public void onNothingSelected(AdapterView<?> arg0) {

   

      

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值