Android核心技术之(3)上---系统组件component

1.UI的定义

•全称user interface, 意为:用户界面
•UI由View和ViewGroup组成       
•View类是所有视图(包括ViewGroup)的根基类
•View在屏幕上占据一片矩形区域,并会在上面进行内容绘制
•ViewGroup包含一些View或ViewGroup,用于控制子View的布局

2.UI事件

•当用户通过手指触摸UI时,系统会自动创建对应的Event对象
•Android中提供了多种方式拦截处理不同类型的事件
•视图本身就可以处理发生在该视图上的事件
•Android提供了很多不同类型的事件监听器接口

View.OnClickListener:  onClick()

View.OnLongClickListener: onLongClick()

View.OnTouchListener: onTouch()

View.OnCreateContextMenuListener: onCreateContextMenu()

View.OnFocusChangeListener:  onFocusChange()

View.OnKeyListener:  onKey()

•给视图添加事件监听的方式

       view.seton…Listener(listener)


3.简单常用的组件(Component)

TextView(文本视图)、EditText(编辑框)、Button(按钮)、ImageView(图片视图)、CheckBox(多选框)、RadioGroup(单选择框组)、RadioButton(单选按钮)

4.TextView(文本视图)

<TextView
    android:id="@+id/tv_simple_message"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="这是TextView的内容"
    android:background="#999999"
    android:textColor="#ff0000"
    android:textSize="20sp"/>

tv_simple_message = (TextView) findViewById(R.id.tv_simple_message);
tv_simple_message.setText("何方勇");

5.EditText(编辑框)

<EditText
    android:id="@+id/et_simple_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入手机号"
    android:inputType="phone">
</EditText>

et_simple_number = (EditText) findViewById(R.id.et_simple_number);


6.Button(按钮)

<Button
    android:id="@+id/btn_simple_submit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="提交" />

btn_simple_submit = (Button) findViewById(R.id.btn_simple_submit);
btn_simple_submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //得到内容
        String number = et_simple_number.getText().toString();
        //提示
        Toast.makeText(SimpleComponentActivity.this, number, 0).show();
    }
});

7.ImageView(图片视图)

<ImageView
    android:id="@+id/iv_simple_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:drawable/alert_dark_frame"
    android:src="@android:drawable/ic_media_play"/>
 
 
iv_simple_icon = (ImageView) findViewById(R.id.iv_simple_icon);
iv_simple_icon.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //设置背景图片
        iv_simple_icon.setBackgroundResource(android.R.drawable.alert_light_frame);
        //设置前景图片
        iv_simple_icon.setImageResource(android.R.drawable.ic_media_pause);
    }
});



8.CheckBox(多选框)

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="爱好: " />

    <CheckBox
        android:id="@+id/cb_simple_basket"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="篮球" />
    <CheckBox
        android:id="@+id/cb_simple_foot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="足球" />

    <CheckBox
        android:id="@+id/cb_simple_pingpang"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="乒乓" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确定"
        android:onClick="confirm"/>

</LinearLayout>

cb_simple_basket = (CheckBox) findViewById(R.id.cb_simple_basket);
cb_simple_foot = (CheckBox) findViewById(R.id.cb_simple_foot);
cb_simple_pingpang = (CheckBox) findViewById(R.id.cb_simple_pingpang);
//cb_simple_foot设置选中状态改变的监听
cb_simple_foot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked) {
            Toast.makeText(SimpleComponentActivity.this, "选中了足球", 0).show();
        } else {
            Toast.makeText(SimpleComponentActivity.this, "未选中足球", 0).show();
        }
    }
});


9.RadioGroup(单选择框组)、RadioButton(单选按钮)

<RadioGroup
    android:id="@+id/rg_simple_sex"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/rb_simple_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

    <RadioButton
        android:id="@+id/rb_simple_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:checked="true"/>
</RadioGroup>


rg_simple_sex = (RadioGroup) findViewById(R.id.rg_simple_sex);
rg_simple_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {//checkedId 选中的radioButtonid
        //找到选中的radioButton
        RadioButton radioButton = (RadioButton) findViewById(checkedId);
        //得到文本
        String sex = radioButton.getText().toString();
        //提示
        Toast.makeText(SimpleComponentActivity.this, sex, 0).show();
    }
});

10.Component(OptionMenu\ContentMenu)(菜单)

•OptionMenu在点击手机的menu触发

•Activity: onCreateOptionsMenu(Menu menu)
•显示OptionMenu的回调方法, 在此方法中向Menu中添加MenuItem
•添加menuItem的两种方式:
•纯编码方式:  menu.add(….)
•加载menu文件的方式:

  MenuInflater menuInflater = getMenuInflater();

  menuInflater.inflate(R.menu.main_option, menu);

•Activity : onOptionsItemSelected(MenuItem item)
•当选择某个菜单项的回调方法


View : setOnCreateContextMenuListener(listener)

为某个视图添加创建ContextMenu的监听(需要长按触发)

Activity : onCreateContextMenu(menu, view, menuInfo)

显示菜单的回调方法

Activity : onContextItemSelected(MenuItem item)

当选择某个菜单项的回调方法



<Button
    android:id="@+id/btn_test2_show_cm"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="显示ContextMenu" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="1. 点击menu显示选项菜单\n2. 长按按钮显示上下文菜单"
    android:textSize="25dp" />

11.Progressbar(进度条)/Seekbar(可手动滑动的进度条)

<ProgressBar        //默认为圆形进度条

            android:id="@+id/pb_test3_loading1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" />

<ProgressBar

        android:id="@+id/pb_test3_loading2"

        style=“?android:attr/progressBarStyleHorizontal“   //水平进度条

        android:layout_width="match_parent“

        android:layout_height="wrap_content"

        android:progress=“2“     //当前进度,默认为0

        android:max=“10”/>      // 最大进度, 默认为100

•ProgressBar

    void setProgress(int Progress) :设置当前进度

     intgetProgress() : 得到当前进度

    void setMax(int max) : 设置最大进度

     intgetMax() : 设置或得到最大进度

•View

     void setVisibility(intvisibility) : 设置视图的可见性

    View. VISIBLE : 标识可见

    View. INVISIBLE : 标识不可见, 但占屏幕空间

    View.GONE : 标识不可见, 也不占屏幕空间


<SeekBar

        android:id="@+id/sb_test3_prgress"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />


SeekBar:

        setOnSeekBarChangeListener(OnSeekBarChangeListener l)  : 设置改变的监听

OnSeekBarChangeListener:

        onProgressChanged(SeekBar seekBarint progress, boolean fromUser) : 进度改变

        onStartTrackingTouch(SeekBar seekBar) : 按下滑杆

        onStopTrackingTouch(SeekBar seekBar) : 从滑杆离开


12.Dialog(对话框)

AlertDialog(警告框)  :

     show()  : 显示警告框

    没有公开的构造方法, 只能通过其内部类Builder来创建

AlertDialog.Builder:  

    create() : 创建AlertDialog对象

    show() : 创建AlertDialog对象, 同时将其显示出来

    setTitle(CharSequence title): 设置标题

    setMessage(CharSequence message) : 设置内容

    setPositiveButton(String text, OnClickListener listener) : 设置正面按钮

    setNegativeButton(String text, OnClickListener listener): 设置负面按钮

    dismiss(): 移除dialog

    setSingleChoiceItems(….)设置单选项列表


自定义对话框:

DialogBuilder  :

     setView(Viewview)  : 设置Dialog中的视图

View:

     View inflate(Context context, int resource, ViewGroup root) : 动态加载布局得到View


带进度条的对话框:

ProgressDialog  :

     staticshow(Context context, CharSequence title,CharSequence message)  : 显示dialog

    

    ProgressDialog(Context context) : 构造方法

     setProgressStyle(int style) 设置样式

     ProgressDialog.STYLE_HORIZONTAL :水平进度条样式


DateDialog(日期对话框):

 public DatePickerDialog(Context context,

            OnDateSetListener callBack,  //点击确定的回调监听

            int year,                                       //显示年份

            int monthOfYear,                      // 显示月分

            int dayOfMonth)                      // 显示日


TimeDialog(时间对话框):

 public TimePickerDialog (Contextcontext,

            OnTimeSetListener callBack,  //点击确定的回调监听

            int hourOfDay                          //几点

            int minute,                                 // 几分

            boolean is24HourView)          // 是否是24小时制





菜单Component

菜单Component





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值