Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 下拉列表,日期選擇器,時間選擇器,單項選擇,多項選擇

下拉列表,日期選擇器,時間選擇器,單項選擇,多項選擇

編輯:關於Android編程

1.下拉列表Spinner

1.1.activity_main.xml

 




    
        

        
    


    

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(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(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

 




    
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.布局

 




    
        
        
    
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.布局

 





    

    
        
        
        
        
    
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.布局

 




    

    

    

    

    

    

    

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);
    }
}
\\

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved