zoukankan      html  css  js  c++  java
  • Android常用控件

    1.RadioButton 、 CheckBox

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        tools:context="com.example.mars_0900_layout05.MainActivity"
        tools:ignore="MergeRootFrame" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="hello" />
    
        <RadioGroup
            android:id="@+id/genderGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <RadioButton
                android:id="@+id/femaleButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="female" />
    
            <RadioButton
                android:id="@+id/maleButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="male" />
        </RadioGroup>
    
        <CheckBox
            android:id="@+id/swim"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="swim" />
    
        <CheckBox
            android:id="@+id/run"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="run" />
    
        <CheckBox
            android:id="@+id/read"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="read" />
    
    </LinearLayout>
    private RadioGroup genderGroup = null;
        private RadioButton femaleButton = null;
        private RadioButton maleButton = null;
        private CheckBox swimBox = null;
        private CheckBox runBox = null;
        private CheckBox readBox = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            genderGroup = (RadioGroup) findViewById(R.id.genderGroup);
            femaleButton = (RadioButton) findViewById(R.id.femaleButton);
            maleButton = (RadioButton) findViewById(R.id.maleButton);
    
            swimBox = (CheckBox) findViewById(R.id.swim);
            runBox = (CheckBox) findViewById(R.id.run);
            readBox = (CheckBox) findViewById(R.id.read);
    
            genderGroup
                    .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            if (femaleButton.getId() == checkedId) {
                                System.out.println("famale");
                                Toast.makeText(MainActivity.this, "famale",
                                        Toast.LENGTH_SHORT).show();//弹出提示
                            } else if (maleButton.getId() == checkedId) {
                                System.out.println("male");
                            }
                        }
                    });
    
            swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (isChecked) {
                        System.out.println("swim is checked");
                    } else {
                        System.out.println("swim is unchecked");
                    }
                }
            });
    
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment()).commit();
            }
        }

    2.进度条ProgressBar

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.example.mars_1000_control02.MainActivity"
        tools:ignore="MergeRootFrame" >
    
        <ProgressBar
            android:id="@+id/firstBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:visibility="gone" />
    
        <ProgressBar
            android:id="@+id/secondBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone" />
    
        <Button
            android:id="@+id/myButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="begin" />
    
    </LinearLayout>


    public class MainActivity extends Activity {
    private ProgressBar firstBar = null;
    private ProgressBar secondBar = null;
    private Button myButton = null;
    private int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    firstBar = (ProgressBar) findViewById(R.id.firstBar);
    secondBar = (ProgressBar) findViewById(R.id.secondBar);
    myButton = (Button) findViewById(R.id.myButton);
    myButton.setOnClickListener(new ButtonListener());
    System.out.println(firstBar.getMax());
    }

    class ButtonListener implements OnClickListener {
    @Override
    public void onClick(View v) {
    if (i == 0) {
    firstBar.setVisibility(View.VISIBLE);
    secondBar.setVisibility(View.VISIBLE);
    } else if (i < 100) {
    firstBar.setProgress(i);//设置进度
    //firstBar.setSecondaryProgress(i + 10);//设置第二进度
    secondBar.setProgress(i);
    } else {
    firstBar.setVisibility(View.GONE);
    secondBar.setVisibility(View.GONE);
    }
    i+=10;
    }

    }

    3.列表控件ListView

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        tools:context="com.example.mars_1000_control02.Activity02"
        tools:ignore="MergeRootFrame" >
    
        <LinearLayout
            android:id="@+id/listLinearLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <ListView
                android:id="@id/android:list"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:drawSelectorOnTop="false"
                android:scrollbars="vertical" >
            </ListView>
        </LinearLayout>
    
    </LinearLayout>


    public class Activity02 extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity02);

    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map1 = new HashMap<String, String>();
    HashMap<String, String> map2 = new HashMap<String, String>();
    HashMap<String, String> map3 = new HashMap<String, String>();
    map1.put("user_name", "zhangsan");
    map1.put("user_ip", "192.168.0.1");
    map2.put("user_name", "lisi");
    map2.put("user_ip", "192.168.0.2");
    map3.put("user_name", "wangwu");
    map3.put("user_ip", "192.168.0.3");
    list.add(map1);
    list.add(map2);
    list.add(map3);
    SimpleAdapter listAdapter = new SimpleAdapter(this, list,
    R.layout.user, new String[] { "user_name", "user_ip" },
    new int[] { R.id.user_name, R.id.user_ip });
    setListAdapter(listAdapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    System.out.println("id------------" + id);
    System.out.println("position------" + position);
    }

  • 相关阅读:
    SQL语法:查询此表有另外一个表没有的数据
    .NET平台开源项目速览-最快的对象映射组件Tiny Mapper之项目实践
    win7 64 安装Oracle 11G 、使用PLSQL进行连接 标准实践
    json 筛选数据 $.grep过滤数据
    bootstrap table 行号 显示行号 添加行号 bootstrap-table 行号
    ajax 请求二进制流 图片 文件 XMLHttpRequest 请求并处理二进制流数据 之最佳实践
    JS中判断JSON数据是否存在某字段的方法 JavaScript中判断json中是否有某个字段
    json 数字key json 数字作为主键
    ajax 跨域 headers JavaScript ajax 跨域请求 +设置headers 实践
    扩展:gridview 空数据时显示表头
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3637400.html
Copyright © 2011-2022 走看看