Android基础控件进阶 必看!!

1.Toast

Toast是Android系统提供的轻量级信息提醒机制,用于向用户提示即时消息,它显示在应用程序界面的最上层,显示一段时间后自动消失不会打断当前操作,也不获得焦点。

1.1Toast的使用

使用Toast提示信息的实例代码:

Toast.makeText(Context,Text,Time),show();
  1. 这段代码首先调用了Toast的makeText方法用来设置提示信息,
  2. Context:表示应用程序环境的信息,就是当前组件的上下文环境,如果在Activity中使用的话,那么该参数可设置为"Activity.this",
  3. Text:表示你需要提示的信息,
  4. Time:表示提示信息的时长,其属性值有"LENGTH_SHORT 和 LENGTH_LONG",这两个值分别表示“短时间”,“长时间”,
  5. 然后调用了show方法来讲提示信息显示到界面中。
Toast.makeText(MainActivity.this,"WIFI已断开",Toast.LENGTH_SHORT).show();

2.点击事件处理

Android点击事件四种写法

2.1(不推荐)结合layout文件声明方法

先在LoginActivity.java中加入btnClick方法:

public void btnClick(View view){
    //参数 Context, text, duration
    //上下文, 要提示的信息, 提示显示的时长
    Toast.makeText(LoginActivity,this, "提示:你点击了按钮",Toast.LENGTH.SHORT).show();
}
然后在布局文件`activity_login.xml`中加入:

```bash
<!--登录按钮-->
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@color/colorWhite"
    android:textSize="22sp"
    android:onClick="btnClick"
    android:text="登 录"/>

**事件写法(后面三种写法)的思路:

  1. 根据控件的ID去获取控件
  2. 设置控件的事件(例如:点击事件)
  3. 在事件的方法中处理事件**

2.2内部实现类完成点击事件

在这里插入图片描述activity_login.xml中加上

<Button
    android:id="@+id/btn_login_submit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@color/colorWhite"
    android:textSize="22sp"
    android:text="登 录"/>

之后在LoginActivity这个类中实现一个ClickListener

Class ClickListener implements View.onClickListener{
    @Override
    public void onClick(View v){
        int id = v.getId();
        switch(id) {
            case R.id.btn_login_submit:
                Toast.makeText(LoginActivity.this,"提示:你点击了按钮,内部实现类",Toast.LENGTH.LONG).show();
                break;
        }
    }
}

之后在LoginActivity的Oncreate中创建内部实现类的对象并设置点击事件

@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
    super.OnCreate(savedInstanceState);
    //设置布局
    setContentView(R.layout.activity_login);
    //获取控件
    Button btnSubmit = findViewById(R.id.btn_login_submit);
    //②创建内部实现类
    ClickListener clickListener = new ClickListener();
    //②设置点击事件
    btnSubmit.setOnClickListener(clickListener);

}

2.3 Activity实现OnClickLitener

//直接上代码:
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置布局
        setContentView(R.layout.activity_login);
        //获取控件
        Button btnSubmit = findViewById(R.id.btn_login_submit);
        //设置点击事件
        btnSubmit.setOnClickListener(this);
    }
    
    // 重载OnClickListener
    @Override
    public void onClick(View view){
        switch(view.getId()){
            case R.id.btn_login_submit:
                Toast.makeText(LoginActivity.this, 
                               "提示:你点击了按钮,Activity实现了OnClickLitener",Toast.LENGTH_LONG).show();
        }
    }
}

2.4匿名内部实现类

public class LoginActivity extends AppCompatActivity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState){
            
            //设置布局
            setContentView(R.layout.activity_login);
            
            //获取控件
            Button btnSubmit = findViewById(R.id.btn_login_submit);
            
            //匿名内部类实现的方式
            btnSubmit.setOnClickListener(new View.OnClickListener){
                @Override;
                public void onClick(View view){
                    Toast.makeText(LoginActivity.this, 
                                   "提示:你点击了按钮,匿名的内部实现类",Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}

3.ImageButton

3.1 ImageButton简介:

ImageButton显示了带有图像(而不是文本)的按钮,用户可以按下或单击该按钮。

3.2 ImageButton的基本使用

属性:
在这里插入图片描述案例:

 <ImageButton 						//使用ImageButton标签
     android:id="@+id/img_button"   // ImageButton的ID,和Button的ID一样
     android:layout_height="wrap_content" //ImageButton的宽和高属性,
     android:layout_width="wrap_content"
     android:src=@drawable/img_name > 

   //ImageButton上显示的图片,表示从图片资源文件中读取名字为img_name的图片

4. RadioButton

4.1简介:

  • RadioButton 为单选框,存在多个选项情况下,需要将 RadioButton 放到 RadioGroup 组中使用,从而实现单选功能。
  • RadioGroup 中可以通过 orientation 来控制排列方式,水平还是垂直。
  • 在这里插入图片描述在这里插入图片描述

4.2RadioButton的基本使用

在这里插入图片描述

案例:

<!-- RadioGroup -->
<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:checkedButton="@id/man">
    <!-- RadioButton -->
    <RadioButton
        android:id="@+id/man"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="16sp"/>
    <!-- RadioButton -->
    <RadioButton
        android:id="@+id/woman"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="16sp"/>
    <!-- RadioButton -->
    <RadioButton
        android:id="@+id/none"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="16sp"/>
</RadioGroup>

4.3 点击事件

RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
    //第一种获得单选按钮值的方法 
    //为radioGroup设置一个监听器:setOnCheckedChanged() 
    radgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        RadioButton radbtn = (RadioButton) findViewById(checkedId);
        Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了" + radbtn.getText(), 		Toast.LENGTH_LONG).show();
     }
  });

5.CheckBox

5.1CheckBox基本使用:

在这里插入图片描述案例:

<CheckBox  
       android:id="@+id/checkbox1"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_above="@+id/button1"  
       android:layout_alignLeft="@+id/linearLayout1"   
       android:text="牛仔" />  

5.2点击事件

代码:

<CheckBox  
        android:id="@+id/cb_one"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_above="@+id/button1"  
        android:layout_alignLeft="@+id/linearLayout1"   
        android:text="牛仔"/>  
  
<CheckBox  
        android:id="@+id/cb_two"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/checkbox3"  
        android:layout_alignBottom="@+id/checkbox3"  
        android:layout_marginLeft="27dp"  
        android:layout_toRightOf="@+id/checkbox3"  
        android:text="面包" />  

<CheckBox  
        android:id="@+id/cb_three"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignBaseline="@+id/checkbox1"  
        android:layout_alignBottom="@+id/checkbox1"  
        android:layout_toRightOf="@+id/button1"  
        android:text="黄油" /> 

java代码:

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
  private CheckBox cb_one;
  private CheckBox cb_two;
  private CheckBox cb_three;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cb_one = (CheckBox) findViewById(R.id.cb_one);
    cb_two = (CheckBox) findViewById(R.id.cb_two);
    cb_three = (CheckBox) findViewById(R.id.cb_three);
    cb_one.setOnCheckedChangeListener(this);
    cb_two.setOnCheckedChangeListener(this);
    cb_three.setOnCheckedChangeListener(this);
 }
  @Override
  public void onCheckedChanged(CompoundButton compoundButton, boolean b){
   if(compoundButton.isChecked())
		Toast.makeText(this,compoundButton.getText().toString(),Toast.LENGTH_SHORT).show();
 }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值