Android中的常用控件

Android常用控件

ImageView

ImageView是用来在界面上展示图片的一个控件,它封装了一些方法,用来显示图片,并且还可以通过设置一些布局中的样式来设置其显示的方式.

常用属性

src:用来设置显示的图片的地址,一般都是指定drawable或者mipmap文件夹中的图片

scaleType:用来设置图片在ImageView中显示的样式

常用方法

setImageResource(int ResourceId);//设置ImageView上显示的图片地址

AlertDialog

AlertDialog是用来弹出提示的对话框,常用于给用户提醒或者让用户确认操作.

实例操作

AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                    dialog.setTitle("This is a dialog");
                    dialog.setMessage("Something Important!");
                    dialog.setCancelable(false);//设置当前按钮不能通过返回键取消
                    //给表示确定的按钮设置上面显示的文字,和点击事件
                    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
                        }
                     );
                    //给表示取消的按钮设置上面显示的文字,和点击事件
                    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(MainActivity.this, "Cancel", Toast.LENGTH_SHORT).show();
                        }
                    });
                    dialog.show();//把对话框显示出来

ProgressBar

ProgressBar是用来显示某一项操作的进度条,常用于给用于显示当前任务的完成进度,通常不可打断.

常用方法

setProgress(int Progress):void 设置当前对话框显示的进度
getProgress():int:获取当前对话框显示的进度

实例操作

      if (proBar.getProgress() != 100)//如果当前进度没有达到100,那么可以继续加,否则清0
          proBar.setProgress(proBar.getProgress() + 10);
      else
          proBar.setProgress(0);
      break;

ProgressDialog

ProgressDialog是用来显示当前进度的对话框,常用于给用户显示任务完成的进度,一般不可打断,但有时可取消.

实例操作

     ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
     progressDialog.setTitle("This is a ProgressDialog!");//设置对话框上显示的标题
     progressDialog.setMessage("Loading...");//设置对话框上面显示的Message
     progressDialog.setCancelable(true);//设置无法取消
     progressDialog.show();//将对话框显示出来

RadioButton和RadioGroup

RadioGroup是Android中的单选控件,一般用来多项只能选一项的时候使用,当使用RadioButton的时候,外部的容器必须是RadioGroup,RadioGroup的用法和LinearLayout的用法相同,出了用来做RadioButton专用容器外,没有什么特殊的地方.

举个例子,先看看我们前台布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="me.worldskills.android.android_ui_controls_test.MainActivity">

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

        <RadioButton
            android:id="@+id/main_rbMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:checked="true"
            android:text="Male"/>

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

    </RadioGroup>

    <Button
        android:id="@+id/main_btnGetChecked"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get Checked"
        android:textAllCaps="false"/>
        
</LinearLayout>

这里默认设置了Male这个RadioButton为选中状态,如果外面的容器不是RadioGroup的话,那么所有的单选按钮可以同时选择.

现在我们需要实现一个功能,就是监听选中的RadioButton的变换

那么,我们最简单的方法就是给它们的外部容器RadioGroup注册事件

我们第一步当然也是需要获得所有界面中使用的控件的实例:

final RadioGroup rbGroup = (RadioGroup) findViewById(R.id.rGroup1);//RadioGroup对象
RadioButton rbMale = (RadioButton) findViewById(R.id.main_rbMale);
RadioButton rbFemale = (RadioButton) findViewById(R.id.main_rbFemale);
Button btnGetChecked = (Button) findViewById(R.id.main_btnGetChecked);

然后我们需要给RadioGroup对象注册选择选择改变的监听器,然后重写onCheckedChanged方法

rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
           RadioButton rb = (RadioButton) findViewById(i);
           Toast.makeText(MainActivity.this, "You choose:" + rb.getText(), Toast.LENGTH_SHORT).show();
            }
        });

这个方法中的第一个参数是当前的RadioGroup,而第二个参数是当前的RadioGroup中选中的RadioButton的id.

在这里,我们必须注意一点,就是所有用到的RadioButton都需要设置id,否则不仅无法实现单选功能,并且这里获取id的时候可能出现错误.

我们还可以通过循环一个RadioGroup里面的RadioButton对象,获取选中的RadioButton

btnGetChecked.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        for (int i = 0; i < rbGroup.getChildCount(); i++) {
            RadioButton rb = (RadioButton) rbGroup.getChildAt(i);
            if (rb.isChecked()) {
                Toast.makeText(MainActivity.this, "Your chosen is " + rb.getText(), Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

CheckBox

CheckBox是Android中的复选框控件,一般用来多项选择的时候使用

先贴上布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="me.worldskills.android.android_ui_controls_test.MainActivity">

    <CheckBox
            android:id="@+id/cbExcel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Excel"/>

    <CheckBox
            android:id="@+id/cbWord"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Word"/>

    <CheckBox
            android:id="@+id/cbPPT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="PPT"/>

    <Button
            android:id="@+id/main_btnSubmit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Submit"
            android:textAllCaps="false"/>
        
</LinearLayout>

我们现在来实现两个很简单的功能,一个是获取选中项,另一个是选中某项的时候输出选中项的文本.

首先,还是需要先得到所有界面上控件的实例:

final CheckBox cbExcel = (CheckBox) findViewById(R.id.cbExcel);
final CheckBox cbWord = (CheckBox) findViewById(R.id.cbWord);
final CheckBox cbPPT = (CheckBox) findViewById(R.id.cbPPT);
Button btnSubmit = (Button) findViewById(R.id.main_btnSubmit);

然后先实现获得选中项的功能,这个功能很简单,直接贴代码了:

btnSubmit.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
          String chosen_items = "";
          if (cbWord.isChecked())
              chosen_items += cbWord.getText() + ",";
          if (cbExcel.isChecked())
              chosen_items += cbExcel.getText() + ",";
          if (cbPPT.isChecked())
              chosen_items += cbPPT.getText() + ",";
          chosen_items = chosen_items.substring(0, chosen_items.length() - 1);
          Toast.makeText(MainActivity.this, chosen_items, Toast.LENGTH_SHORT).show();
       }
    });

然后我们来实现第二个功能,就是当某项选中的时候,实现输出选中项文本,这时候你肯定已经想到了设置一个CheckedChangeListener,但是我们有三个复选框,一个一个设置太麻烦了,所以我们选择让当前Activity实现CompoundButton.OnCheckedChangeListener 接口,然后直接重写onCheckedChanged方法,这样,就可以设置监听器是当前活动了,代码贴出来:

当前对象实现接口:

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener

设置监听器为当前对象:

cbExcel.setOnCheckedChangeListener(this);
cbWord.setOnCheckedChangeListener(this);
cbPPT.setOnCheckedChangeListener(this);

重写onCheckedChanged方法

@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  if (compoundButton.isChecked())
      Toast.makeText(this, compoundButton.getText(), Toast.LENGTH_SHORT).show();
}

其中compoundButton其实就是传递过来的CheckBox对象,因为CheckBoxRadioButton都继承自compoundButton.

Everything has its time and that time must be watched. If you have any question, please contact email linqtosql@qq.com.
原文地址:https://www.cnblogs.com/Fill/p/7343861.html