Android-常用控件

1. WebView

    WebView wv = (WebView) findViewById(R.id.web_view);
    //支持 JavaScript 脚本
    wv.getSettings().setJavaScriptEnabled(true);
    //防止跳转到系统浏览器
    wv.setWebViewClient(new WebViewClient());
    //加载指定网址
    wv.loadUrl("http://www.baidu.com");

2. ScrollView

  • 能包含一个子控件,若想添加多个控件,可添加一个布局

问题解决:自动显示到中间位置;

修改父布局属性

android:focusable="true" android:focusableInTouchMode="true"

3. DataPickerDialog

//日期选择器
final Calendar calendar = Calendar.getInstance(); //当前日期
DatePickerDialog dialog = new DatePickerDialog(DatePickerDialogActivity.this, new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        calendarChoose.set(year,month,day);
        String mDay=Integer.toString(day);
        if(day<10){ //格式化
            mDay="0"+mDay;
        }
        tv.setText(year + "-" + (month + 1) + "-" + mDay);
    }    //下方这几个值为日期选择器初始值
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
dialog.show();

注意: month 默认是从0开始算起

4. TimePickerDialog

   //时间选择器
    TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
            String mHour=Integer.toString(hourOfDay) ;
            String mMinute=Integer.toString(minute) ;
            if(hourOfDay<10){
                mHour="0"+hourOfDay;
            }
            if(minute<10){
                mMinute="0"+minute;
            }
            tv.setText(mHour+":"+mMinute);
        }
    },0,0,true);  //设置初始值 为 00:00
    timePickerDialog.show();        

5. ViewFlipper

简单使用: 直接添加ImageView

flipInterval 自动切换时间,


    
    
    

   ViewFlipper viewFlipper=(ViewFlipper)findViewById(R.id.viewFilpper);
   viewFlipper.startFlipping(); //开始    

6. ImageView

src属性和background属性的区别

src填入图片时,是按照图片的大小直接填充,并不会进行拉伸。

而是用background填入图片时,则会根据ImageView给定的宽度来进行拉伸。

7.Spinner

下拉菜单


设置Adapter

String[] datas={"Item 1","Item 2","Item 3","Item 4","Item 5"};
ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,datas);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

关于 setDropDownViewResource 设置下拉列表项的布局样式

代码设置选中项:

spinner.setSelection(2);

选中事件:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView adapterView, View view, int i, long l) {
        spinner.getSelectedItem().toString(); //选中项文本
        spinner.getSelectedItemId(); //选中项文本
        spinner.getSelectedItemPosition(); //选中项位置,从1开始
    }

    @Override
    public void onNothingSelected(AdapterView adapterView) {
    }
});

8.AutoCompleteTextView

常用属性:

completionThreshoid 指定用户输入多少个字符后才会显示提示
completionHint 设置下拉菜单的提示标题
dropDownHeight 设置下拉菜单的高度
dropDownWidth 设置下拉菜单的宽度
popuBackground 设置下拉菜单的背景

设置Adapter:

 String[] datas={"123","12","11111","1111222","2223333"};

        AutoCompleteTextView  acTv=(AutoCompleteTextView)findViewById(R.id.activity_test_auto_tv);

        ArrayAdapter adapter=
                new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,datas);
        acTv.setAdapter(adapter);

9.SeekBar

常用属性:

max 滑动条的最大值
progress 滑动条的当前值
thumb 滑块的drawable

事件:

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    //进度发生改变时触发
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        tvSeekBarInfo.setText("当前进度值 "+seekBar.getProgress()+"/"+seekBar.getMax());
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) { //按住SeekBar触发

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) { //松开SeekBar触发

    }
});

10.ProgressDialog

        progressDialog = new ProgressDialog(mContext);
        progressDialog.setCancelable(true); //设置是否可以通过点击Back取消
        progressDialog.setCanceledOnTouchOutside(true);//点击Dialog外区域取消显示Dialog进度条
        progressDialog.setMessage(""); //设置显示的内容
        progressDialog.setTitle(""); //设置标题
        progressDialog.setIcon(R.mipmap.ic_launcher); //Tittle的图标,如果设置Title的话Icon是不会显示的

        //ProgressDialog.STYLE_HORIZONTAL 水平进度条
        //ProgressDialog.STYLE_SPINNER  圆形进度条
        progressDialog.setProgressStyle(STYLE_HORIZONTAL); //设置进度条显示样式
        progressDialog.setMax(30);  //这是进度条最大值
        progressDialog.setProgress(1);  //设置进度条的进度值


        //监听 Dismiss
        progressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {

            }
        });

        //设置可点击的按钮,默认情况下有三个可选值
        //DialogInterface.BUTTON_POSITIVE  //确定
        //DialogInterface.BUTTON_NEGATIVE  //取消
        //DialogInterface.BUTTON_NEUTRAL   //中立
        progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        progressDialog.dismiss();
                    }
                });

11.RadioButton

若果须要实现单选功能,需要报RadioButton放到RadioGroup中,通过RadioGroup的orientation属性来设置排列方向

获取选中RadioButton的两种方式:

      RadioGroup rg=(RadioGroup)findViewById(R.id.activity_test_rg);   //选中后获取
      rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rbtn=(RadioButton)findViewById(checkedId);
                Toast.makeText(TestActivity.this, rbtn.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    //手动获取(也就是需要点击Button,必须有默认选中
    RadioGroup rg=(RadioGroup)findViewById(R.id.activity_test_rg); 
    RadioButton rbtn=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
    Toast.makeText(TestActivity.this, rbtn.getText(), Toast.LENGTH_SHORT).show();

你可能感兴趣的:(Android-常用控件)