Android - 基础控件

TextView

显示文本控件

//在xml中的代码:
    

//android:layout_width 和android:layout_height是所有控件都有的属性,可选值有:
//match_parent、fill_parent:当前控件大小和父布局大小相同
//wrap_content :内容决定控件大小
Button

按钮控件

    
//第一种方法添加点击事件
        Button button = (Button)findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //添加点击事件
            }
        });
//第二种方法添加点击事件
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thrid);
        Button button = (Button)findViewById(R.id.button2);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        //添加点击事件
    }
EditText

编辑文本控件
在xml文件中用法相似,只写出区别属性

android:hint="Type something here" //提示文本 
androis:maxLines="2"  //最大行数
EditText editText=(EditText)findViewById(R.id.edit_text);
String text = editText.getText().toString();
ImageView

显示图片

app:srcCompat="@mipmap/ic_launcher"//显示那张图片
ImageView imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageResource(R.mipmap.ic_launcher_round);
//动态修改显示图片
ProgressBar

进度条显示
android:visibility。指定控件是否显示,可选值
visible:可见。默认值
invisible:不可见,但仍占据着控件原有位置和大小
gone:不可见,不占据位置

//xml文件
style="?android:attr/progressBarStyle" //样式
ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
//progressBar.setVisibility(View.INVISIBLE);
//progressBar.setVisibility(View.GONE);
AlertDialog

弹出提示对话框

        AlertDialog.Builder dialog = new AlertDialog.Builder(ThridActivity.this);
        dialog.setTitle("Dialog");
        dialog.setMessage("Something important.");
        dialog.setCancelable(false);
        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        dialog.show();

你可能感兴趣的:(Android - 基础控件)