Android 常用控件(二)

直接进入正题

1.Button按钮控件
Button控件也是使用过程中用的最多的控件之一,所以需要好好掌握。用户可以通过单击 Button 来触发一系列事件,然后为 Button 注册监听器,来实现 Button 的监听事件。

先来看button的常用属性:

<Button

//控件id
android:id = "@+id/xxx"  @+id/xxx表示新增控件命名为xxx

//宽度与高度
android:layout_width="wrap_content"  //wrap_content或者match_parent
android:layout_height="wrap_content"  //wrap_content或者match_parent

//按钮上显示的文字 
android:text="theButton" //两种方式,直接具体文本或者引用values下面的string.xml里面的元素@string/button

//按钮字体大小
android:textSize="24sp"  //以sp为单位

//字体颜色
android:textColor="#0000FF"  //RGB颜色

//字体格式
android:textStyle="normal"  //normal,bold,italic分别为正常,加粗以及斜体,默认为normal

//是否只在一行内显示全部内容
android:singleLine="true"  //true或者false,默认为false

关于button类别和事件处理
这里写图片描述

Button的点击事件有两种实现方式,其一是在Activity中为Button的点击事件注册一个监听器,还有是调用OnClick方法:
(监听器)

public class MainActivity extends Activity {
    private EditText edittext;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edittext=(EditText) findViewById(R.id.edit_text);
        button = (Button) findViewById(R.id.button);
        //为button按钮注册监听器,并通过匿名内部类实现
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            //点击Button会改变edittext的文字为"点击了Button"
            edittext.setText("点击了Button");
            }
        }); 
    }
}

(OnClick)
使用OnClick属性,直接定义方法名

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btn_up"
        android:onClick="login"
        />

JAVA-MainActivity实现该方法

public void login(View view){
        //代码块
        }

2.RadioButton与RadioGroup
RadioButton(单选按钮)在 Android 平台上也比较常用,比如一些选择项会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在 RadioButton 没有 被选中时,用户通过单击来选中它。但是,在选中后,无法通过单击取消选中。

RadioGroup 是单选组合框,用于 将 RadioButton 框起来。在多个 RadioButton被 RadioGroup 包含的情况下,同一时刻只可以选择一个 RadioButton,并用 setOnCheckedChangeListener 来对 RadioGroup 进行监听。

属性:

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:id="@+id/rgp_app3_sex"
        >

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:id="@+id/rb_app3_boy"
            />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:id="@+id/rb_app3_gril"
            android:layout_centerHorizontal="true"

            />

    </RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="看看"
        android:onClick="look"
        android:layout_marginBottom="10dp"
        />

下面给出在Activity中用 setOnCheckedChangeListener 来对 RadioGroup 进行监听的代码, 注意RadioGroup中的RadioButton也都是需要声明和通过控件的id来得到代表控件的对象。

public class MainActivity extends AppCompatActivity {

    private RadioGroup rgp_app3_sex; 

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

        //获取单选框控件
        rgp_app3_sex = (RadioGroup) findViewById(R.id.rgp_app3_sex);

    }


 //单选
    public void look(View view){
        //获取单选组中被选中的单选框id
        int checked=rgp_app3_sex.getCheckedRadioButtonId();
        //根据单选框id获取单选框
        RadioButton rb_app3_boy = (RadioButton) findViewById(checked);
        String sex=rb_app3_boy.getText().toString();
        Toast.makeText(this, sex, Toast.LENGTH_SHORT).show();
    }

3.CheckBox复选
CheckBox(复选按钮),顾名思义是一种可以进行多选的按钮,默认以矩形表示。与 RadioButton 相同,它也有选中或者不选中双状态。我们可以先在布局文件中定义多选按钮, 然后对每一个多选按钮进行事件监听 setOnCheckedChangeListener,通过 isChecked 来判断 选项是否被选中,做出相应的事件响应。

这里写图片描述

CheckBox的简单使用:

<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="吃"
        android:id="@+id/cb_app3_eat"
        />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="睡"
        android:id="@+id/cb_app3_sleep"
        />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="玩"
        android:id="@+id/cb_app3_play"
        />
public class MainActivity extends AppCompatActivity {


    private CheckBox cb_app3_eat;
    private CheckBox cb_app3_sleep;
    private CheckBox cb_app3_play;
    List<CheckBox> lc=new ArrayList<CheckBox>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取复选框控件
        cb_app3_eat = (CheckBox) findViewById(R.id.cb_app3_eat);
        cb_app3_sleep = (CheckBox) findViewById(R.id.cb_app3_sleep);
        cb_app3_play = (CheckBox) findViewById(R.id.cb_app3_play);
        lc.add(cb_app3_eat);
        lc.add(cb_app3_sleep);
        lc.add(cb_app3_play);
    }


//多选
    public void cnm(View view){

        String hh="";
        for(CheckBox c:lc){
            if(c.isChecked()){
                hh+=c.getText().toString();
            }
        }

        if(hh==""){
            hh="未选中";
        }
        Toast.makeText(this, hh, Toast.LENGTH_SHORT).show();

    }

4.Toast (吐司)
Toast是Android提供的“快显讯息”类,它的用途很多,使用起来不难。

(1)public int getDuration ()

返回存续期间

 请参阅setDuration(int)



(2)public int getGravity ()

 取得提示信息在屏幕上显示的位置。      

 请参阅  Gravity setGravity()


(3)public static Toast makeText (Context context, int resId, int duration)

生成一个从资源中取得的包含文本视图的标准 Toast 对象。

参数:

  context 使用的上下文。通常是你的 Application 或 Activity 对象。

  resId  要使用的字符串资源ID,可以是已格式化文本。       

  duration 该信息的存续期间。值为 LENGTH_SHORT 或 LENGTH_LONG    




(4)public static Toast makeText (Context context, CharSequence text, int duration)

生成一个包含文本视图的标准 Toast 对象。

参数:

context

使用的上下文。通常是你的 Application 或 Activity 对象。

resId

要显示的文本,可以是已格式化文本。

duration

该信息的存续期间。值为 LENGTH_SHORT 或 LENGTH_LONG





(5)public void setDuration (int duration)

设置存续期间。

请参阅

LENGTH_SHORT

LENGTH_LONG



(6)public void setGravity (int gravity, int xOffset, int yOffset)

设置提示信息在屏幕上的显示位置。

(自定义Toast的显示位置,toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0)可以把Toast定位在左上角。Toast提示的位置xOffset:大于0向右移,小于0向左移

请参阅

Gravity

getGravity()




 (7)public void setText (CharSequence s)

更新之前通过 makeText() 方法生成的 Toast 对象的文本内容。

参数s   为 Toast 指定的新的文本



(8)public void setView (View view)

设置要显示的 View 。

(注意这个方法可以显示自定义的toast视图,可以包含图像,文字等等。是比较常用的方法。)

请参阅

getView()



(9)public void show ()

按照指定的存续期间显示提示信息。

简单使用实例:

public void login(View view){
        //获取用户名
        String uname=et_app3_uname.getText().toString();
        String upwd=et_app3_upwd.getText().toString();

            //弹出Toast
            Toast toast = Toast.makeText(this, "six six six:" + uname, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            //加图标
            LinearLayout ll = (LinearLayout) toast.getView();
            ll.setOrientation(LinearLayout.HORIZONTAL);
            ImageView toastimage = new ImageView(getApplicationContext());
            toastimage.setImageResource(R.drawable.jinx_r);
            ll.addView(toastimage, 0);
            toast.show();
        }

5.日期对话框Toast
适用于许多Android时间应用
属性:
这里写图片描述


简单应用:
//日期对话框
    public void getDate(View view){
        Calendar c=Calendar.getInstance();
        int year=c.get(Calendar.YEAR);
        int month=c.get(Calendar.MONTH);
        int day=c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog dpd=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int a, int b, int c) {
                Toast.makeText(MainActivity.this, a+"年"+(b+1)+"月"+c+"日", Toast.LENGTH_SHORT).show();
            }
        },year,month,day){};
        dpd.show();
    }

效果图:
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值