码迷,mamicode.com
首页 > 其他好文 > 详细

下拉列表,日期选择器,时间选择器,单项选择,多项选择

时间:2016-04-29 15:18:38      阅读:407      评论:0      收藏:0      [点我收藏+]

标签:

1.下拉列表Spinner

1.1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.rj141.sb.kongjian.MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18dp"
            android:text="请选择您最喜欢的水果:" />

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinner" />
    </LinearLayout>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:id="@+id/tv" />

</LinearLayout>
Spinner是下拉列表的组件

1.2.MainActivity.class

public class MainActivity extends AppCompatActivity {

    private Spinner s;
    String[] data=new String[]{"苹果","雪梨","西瓜","葡萄","橙子","草莓"};
    private TextView tv;

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

        tv= (TextView) this.findViewById(R.id.tv);
        s= (Spinner) this.findViewById(R.id.spinner);
        s.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data));
        s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String str=data[position];
                tv.setText("最喜欢的水果是:"+str);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }
}
s.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data));android.R.layout.simple_list_item_1是指安卓自带的下拉列表格式,data是数据源;

s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()){..};是下拉列表的监听

技术分享


2.日期选择器

2.1.activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.rj141.sb.kongjian.DateActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:id="@+id/tv" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="日历"
        android:id="@+id/btndate" />
</LinearLayout>
2.2.DateActivity.class

public class DateActivity extends ActionBarActivity {

    private Button btn;
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date);

        btn=(Button)this.findViewById(R.id.btndate);
        tv= (TextView) this.findViewById(R.id.tv);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(DateActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        tv.setText("您的出生日期是:"+String.format("%d-%d-%d",year,monthOfYear+1,dayOfMonth));
                    }
                },2000,1,2).show();
            }
        });
    }
}
DatePickerDialog日历选择器的对话框,监听为OnDateSetListener(){..}

技术分享技术分享技术分享


3.时间选择器

3.1.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.rj141.sb.kongjian.DateActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="几点吃饭:"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:id="@+id/tv" />
    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="时间"
        android:id="@+id/btndate" />
</LinearLayout>
3.2.Java文件

public class DateActivity extends ActionBarActivity {

    private Button btn;
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date);

        btn=(Button)this.findViewById(R.id.btndate);
        tv= (TextView) this.findViewById(R.id.tv);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TimePickerDialog(DateActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        tv.setText(String.format("%d:%d",hourOfDay,minute));
                    }
                //0,0指的是时间,true表示是否为24小时,true为24小时制
                },0,0,true).show();
            }
        });
    }
}
技术分享技术分享技术分享


4.单项选择

4.1.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.rj141.sb.kongjian.DateActivity">


    <TextView
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2+3="
        android:textSize="22dp"
        />

    <RadioGroup
        android:layout_marginLeft="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A.2"
            android:id="@+id/rb1"
            />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="B.3"
            android:id="@+id/rb2"
            />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C.4"
            android:id="@+id/rb3"
            />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="D.5"
            android:id="@+id/rb4"
            />
    </RadioGroup>

    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交"/>
</LinearLayout>
4.2.Java文件

public class SingChoose extends AppCompatActivity {
    private Button btn;
    private RadioButton rbD;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sing_choose);

        rbD= (RadioButton) this.findViewById(R.id.rb4);
        btn= (Button) this.findViewById(R.id.submit);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(rbD.isChecked()){
                    Toast.makeText(SingChoose.this,"正确,请加五分",Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(SingChoose.this,"错误,请减五分",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
技术分享技术分享技术分享


5.多项选择

5.1.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.rj141.sb.kongjian.CheckChoose">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="你喜欢下列哪些物品?"
        />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="巧克力"
        android:id="@+id/cb1" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="冰淇淋"
        android:id="@+id/cb2" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="蛋糕"
        android:id="@+id/cb3" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="啤酒"
        android:id="@+id/cb4" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="肉"
        android:id="@+id/cb5" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:id="@+id/tv" />

</LinearLayout>
5.2.Java文件

public class CheckChoose extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private CheckBox cb1,cb2,cb3,cb4,cb5;
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_choose);

        tv= (TextView) this.findViewById(R.id.tv);
        cb1= (CheckBox) this.findViewById(R.id.cb1);
        cb2= (CheckBox) this.findViewById(R.id.cb2);
        cb3= (CheckBox) this.findViewById(R.id.cb3);
        cb4= (CheckBox) this.findViewById(R.id.cb4);
        cb5= (CheckBox) this.findViewById(R.id.cb5);
        cb1.setOnCheckedChangeListener(this);
        cb2.setOnCheckedChangeListener(this);
        cb3.setOnCheckedChangeListener(this);
        cb4.setOnCheckedChangeListener(this);
        cb5.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        String str="您喜欢:";
        if(cb1.isChecked()){
            str+=cb1.getText()+",";
        }
        if(cb2.isChecked()){
            str+=cb2.getText()+",";
        }
        if(cb3.isChecked()){
            str+=cb3.getText()+",";
        }
        if(cb4.isChecked()){
            str+=cb4.getText()+",";
        }
        if(cb5.isChecked()){
            str+=cb5.getText()+",";
        }
        tv.setText(str);
    }
}
技术分享技术分享


下拉列表,日期选择器,时间选择器,单项选择,多项选择

标签:

原文地址:http://blog.csdn.net/qq_29481375/article/details/51275435

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!