posts - 0,  comments - 0,  trackbacks - 0
package level0.exercise.layout;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.MultiAutoCompleteTextView;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 都是一些基本的布局控件的基本实现,依次是:
 * 两个单选按钮组,每组3人单选按钮:RadioGroup&&RadioButton
 * 四个复选框:CheckBox
 * 一个下拉列表:Spinner
 * 一个自动提示和一个多重自动提示:AutoCompleteTextView&&MultiAutoCompleteTextView
 * 
@author level0
 *
 
*/

public class LayoutExercise extends Activity {
    
/** Called when the activity is first created. */
    @Override
    
public void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        RadioGroup rg1 
= (RadioGroup) findViewById(R.id.group1);
        RadioGroup rg2 
= (RadioGroup) findViewById(R.id.group2);

        
// RadioButton rb1 = (RadioButton)findViewById(R.id.choice1);
        
// RadioButton rb2 = (RadioButton)findViewById(R.id.choice2);
        
// RadioButton rb3 = (RadioButton)findViewById(R.id.choice3);
        
// RadioButton rb4 = (RadioButton)findViewById(R.id.choice4);
        
// RadioButton rb5 = (RadioButton)findViewById(R.id.choice5);
        
// RadioButton rb6 = (RadioButton)findViewById(R.id.choice6);

        
final TextView tv1 = (TextView) findViewById(R.id.tv1);
        
final TextView tv2 = (TextView) findViewById(R.id.tv2);
        
final TextView tv3 = (TextView) findViewById(R.id.tv3);
        
        
        rg1.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {

            
public void onCheckedChanged(RadioGroup group, int checkedId) {
                
// TODO Auto-generated method stub
                if (checkedId == R.id.choice1) {
                    showToast(
"1");
                    tv1.setText(
"1");
                } 
else if (checkedId == R.id.choice2) {
                    showToast(
"2");
                    tv1.setText(
"2");
                } 
else if (checkedId == R.id.choice3) {
                    showToast(
"3");
                    tv1.setText(
"3");
                }
            }
        });

        rg2.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {

            
public void onCheckedChanged(RadioGroup group, int checkedId) {
                
// TODO Auto-generated method stub
                if (checkedId == R.id.choice4) {
                    showToast(
"1");
                    tv2.setText(
"1");
                } 
else if (checkedId == R.id.choice5) {
                    showToast(
"2");
                    tv2.setText(
"2");
                } 
else if (checkedId == R.id.choice6) {
                    showToast(
"3");
                    tv2.setText(
"3");
                }
            }
        });
        
        
final CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);
        
final CheckBox cb2 = (CheckBox) findViewById(R.id.cb2);
        
final CheckBox cb3 = (CheckBox) findViewById(R.id.cb3);
        
final CheckBox cb4 = (CheckBox) findViewById(R.id.cb4);
        
        cb1.setOnCheckedChangeListener(
new CheckBox.OnCheckedChangeListener(){

            
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                
// TODO Auto-generated method stub
                if (cb1.isChecked()){
                    showToast(
"1");
                    tv3.setText(
"1");
                }
            }
            
        });
        cb2.setOnCheckedChangeListener(
new CheckBox.OnCheckedChangeListener(){

            
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                
// TODO Auto-generated method stub
                if (cb2.isChecked()){
                    showToast(
"2");
                    tv3.setText(
"2");
                }
            }
            
        });
        cb3.setOnCheckedChangeListener(
new CheckBox.OnCheckedChangeListener(){

            
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                
// TODO Auto-generated method stub
                if (cb3.isChecked()){
                    showToast(
"3");
                    tv3.setText(
"3");
                }
            }
            
        });
        cb4.setOnCheckedChangeListener(
new CheckBox.OnCheckedChangeListener(){

            
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                
// TODO Auto-generated method stub
                if (cb4.isChecked()){
                    showToast(
"4");
                    tv3.setText(
"4");
                }
            }
            
        });
        
        
final String[] choices = {"a""abc""abcd""abcde""abcdef"};
        Spinner spinner 
= (Spinner)findViewById(R.id.spinner);
        ArrayAdapter
<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, choices);
        
        
//这个语句用于设置风格,明显会比不设置要好看,可以通过注释这个语句来看效果
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
        spinner.setAdapter(adapter); 
        spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener(){

            @Override
            
public void onItemSelected(AdapterView<?> arg0, View arg1,
                    
int arg2, long arg3) {
                
// TODO Auto-generated method stub
                TextView tv4 = (TextView)findViewById(R.id.tv4);
                tv4.setText(choices[arg2]);
            }

            @Override
            
public void onNothingSelected(AdapterView<?> arg0) {
                
// TODO Auto-generated method stub
                
            }
            
        });
        
        ArrayAdapter
<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, choices);
        AutoCompleteTextView actv 
= (AutoCompleteTextView) findViewById(R.id.actv);
        actv.setAdapter(adapter1);
        actv.setThreshold(
1);
        MultiAutoCompleteTextView mactv 
= (MultiAutoCompleteTextView) findViewById(R.id.mactv);
        mactv.setAdapter(adapter1);
        mactv.setTokenizer(
new MultiAutoCompleteTextView.CommaTokenizer());
    }

    
private void showToast(String s) {
        Toast.makeText(LayoutExercise.
this, s, Toast.LENGTH_SHORT).show();
    }
}




main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
="vertical"
    android:layout_width
="fill_parent"
    android:layout_height
="fill_parent"
    android:scrollbars
="vertical"
    
>
<LinearLayout
    
android:orientation="vertical"
    android:layout_width
="fill_parent"
    android:layout_height
="fill_parent"
    
> 
<TextView  
    
android:id="@+id/tv1"
    android:layout_width
="fill_parent" 
    android:layout_height
="wrap_content" 
    android:text
="RadioGroup1"
    
/>
<RadioGroup
    
android:id="@+id/group1"
    android:orientation
="vertical"
    android:layout_width
="fill_parent"
    android:layout_height
="wrap_content">
<RadioButton
       
android:id="@+id/choice1"
       android:layout_width
="fill_parent"
       android:layout_height
="wrap_content"
       android:text
="choice1"/>
<RadioButton
       
android:id="@+id/choice2"
       android:layout_width
="fill_parent"
       android:layout_height
="wrap_content"
       android:text
="choice2"/>
<RadioButton
       
android:id="@+id/choice3"
       android:layout_width
="fill_parent"
       android:layout_height
="wrap_content"
       android:text
="choice3"/>
 
</RadioGroup>
 
<TextView  
    
android:id="@+id/tv2"
    android:layout_width
="fill_parent" 
    android:layout_height
="wrap_content" 
    android:text
="RadioGroup2"
    
/>
<RadioGroup
    
android:id="@+id/group2"
    android:orientation
="vertical"
    android:layout_width
="fill_parent"
    android:layout_height
="wrap_content">
<RadioButton
       
android:id="@+id/choice4"
       android:layout_width
="fill_parent"
       android:layout_height
="wrap_content"
       android:text
="choice1"/>
<RadioButton
       
android:id="@+id/choice5"
       android:layout_width
="fill_parent"
       android:layout_height
="wrap_content"
       android:text
="choice2"/>
<RadioButton
       
android:id="@+id/choice6"
       android:layout_width
="fill_parent"
       android:layout_height
="wrap_content"
       android:text
="choice3"/>   
 
</RadioGroup>
 
<TextView
     
android:id="@+id/tv3"
     android:layout_width
="fill_parent"
     android:layout_height
="wrap_content"
     android:text
="CheckBoxs"/>
 
<CheckBox
    
android:id="@+id/cb1"
    android:layout_width
="fill_parent"
    android:layout_height
="wrap_content"
    android:text
="checkBox1"/>
 
<CheckBox
    
android:id="@+id/cb2"
    android:layout_width
="wrap_content"
    android:layout_height
="wrap_content"
    android:text
="checkBox2"/>
 
<CheckBox
    
android:id="@+id/cb3"
    android:layout_width
="wrap_content"
    android:layout_height
="wrap_content"
    android:text
="checkBox3"/>
<CheckBox
    
android:id="@+id/cb4"
    android:layout_width
="wrap_content"
    android:layout_height
="wrap_content"
    android:text
="checkBox4"/>
<TextView
    
android:id="@+id/tv4"
    android:layout_width
="fill_parent"
    android:layout_height
="wrap_content"
    android:text
="Spinner"/>
<Spinner
    
android:id="@+id/spinner"
    android:layout_width
="fill_parent"
    android:layout_height
="wrap_content"/>
<AutoCompleteTextView
    
android:id="@+id/actv"
    android:layout_width
="fill_parent"
    android:layout_height
="wrap_content"
    
/>
<MultiAutoCompleteTextView
    
android:id="@+id/mactv"
    android:layout_width
="fill_parent"
    android:layout_height
="wrap_content"/>
</LinearLayout>
</ScrollView>

posted on 2011-01-02 12:00 level0 阅读(204) 评论(0)  编辑  收藏 所属分类: android日志
<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

留言簿

文章分类

文章档案

搜索

  •  

最新评论