Android-(4)-常用控件

常用控件


TextView

<TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="25sp"   <!--文字大小-->
        android:textColor="#aaf"   <!--文字颜色-->
        android:gravity="center"    <!--文字对齐方式-->
        android:text="TextView" />   <!--指定显示的文本类容-->

Button

<Button
        android:id="@+id/second_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="false"  <!--如果text是因为会全部大写,flase不会-->
        android:text="点击" />

按钮注册监听事件:

之前已经有所涉及,在活动类的OnCreate方法中:

Button sec_button = findViewById(R.id.second_button);
sec_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Toast.makeText(MyActivity.this, "这是第二个活动", Toast.LENGTH_SHORT).show();
    }
});

这是使用匿名类的方式,如果按钮较多,也可使用实现接口的方式,也就是说让活动类继承:

public class MyActivity extends BaseActivity  implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        //要在oncreate中给按钮添加事件
        Button button01 = findViewById(R.id.second_button);
        button01.setOnClickListener(this);
}
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.second_button:{
                //Toast.makeText(MyActivity.this, "这是第二个活动", Toast.LENGTH_SHORT).show();
            }break;
            default:
                break;
        }
    }
}

如果还有其他的按钮,就可以直接增加case语句就行了。


EditView

<EditText
android:id="@+id/editText_sec"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:maxLines="2"
android:hint="writing" />

通过下面代码获取输入的内容:

EditText editText = findViewById(R.id.editText_sec);
String data = editText.getText().toString();

ImageView

用于放图片的控件。图片资源一般放在drawable文件夹中。有的会设置分辨率,比如在res目录下新建一个drawable-xhdpi文件夹。并放一个图片text.png里。

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/test" />

更换图片源,比如说再导入一个图片test02.png

ImageView imageView = findViewById(R.id.imageView_sec);
imageView.setImageResource(R.drawable.test02);

进度条ProgressBar

环形进度条,说明某事正在运行:

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

可以修改style属性变为条形进度条:

style="?android:attr/progressBarStyleHorizontal"

设置条形进度条的最大最小值

android:max="100"
android:min="0"

改变条形进度条的进度值:

ProgressBar progressBar = findViewById(R.id.progressBar_hor);
progressBar.getVisibility() //获取可见度
progressBar.setVisibility(View.GONE); //设置可见度

可见度有三个值:

  • View.VISIBLE 可见
  • View.INVISIBLE 不可见 但保留位置
  • View.GONE 不可见 位置也不保留

进度条的值:

ProgressBar progressBar = findViewById(R.id.progressBar_hor);
int pro = progressBar.getProgress();   //获取进度条的值
pro += 10;
progressBar.setProgress(pro);  //设置进度条的值

警告框AlertDialog

AlertDialog.Builder dialog = new AlertDialog.Builder(MyActivity.this); //一个对象
dialog.setTitle("这是提示框");
dialog.setMessage("这是消息");
//dialog.setCancelable(false); //设置是否有取消,要非常注意,设置为false一定要有代码能退出弹窗

//设置积极按钮
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(v.getContext(), "你点击了OK", Toast.LENGTH_SHORT).show();
    }
});
//设置消极按钮
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(v.getContext(), "你点击了 取消", Toast.LENGTH_SHORT).show();
    }
});

dialog.show();  //显示出来

带进度条的弹窗ProgressDialog

ProgressDialog progressDialog = new ProgressDialog(MyActivity.this);
progressDialog.setTitle("这是ProgressDialog");
progressDialog.setMessage("耐心等待...");
progressDialog.setCancelable(true);  //这里一定要注意,如果为false,代码就一定要做好控制
progressDialog.show();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值