安卓常用UI控件的使用方法

时间:2022-07-17 23:21:01

1.TextView(文本框)

 <TextView
android:id="@+id/text_view"
android:layout_width
="match_parent" <!--和父布局大小一致-->
android:layout_height="wrap_content"
<!--刚好包住里面的内容-->
android:gravity="center"
<!--居中-->
android:text="This is TextView"
android:textColor="#00ff00"
android:textSize="24sp" />

2.Button(按钮)

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

注册监听器

方法一:匿名类

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button
= (Button) findViewById(R.id.button);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
//内容
}
});
}
}

方法二:使用实现接口

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button
= (Button) findViewById(R.id.button);
button.setOnClickListener(
this);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button:
//添加逻辑
break;
default:
break;
}
}
}

3.EditText

允许用户在控件里输入和编辑内容,并可以在程序中对这些内容进行处理

  <EditText
android:id="@+id/edit_text"
android:hint
="Type something there"<!--提示性文本-->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
<!--最大行数为2行-->
/>

按一下按钮,弹出提示文本

public class MainActivity extends AppCompatActivity implements View.OnClickListener //entends类,implement接口
{

private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button
= (Button) findViewById(R.id.button);
editText
= (EditText) findViewById(R.id.edit_text);
button.setOnClickListener(
this);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button:
String inputText
= editText.getText().toString(); //转换成字符串
Toast.makeText(MainActivity.
this,inputText,Toast.LENGTH_SHORT).show();//注意toast的用法
break;
default:
break;
}
}
}

4.ImageView(图片)

  <ImageView
android:id="@+id/image_view"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:src
="@drawable/img_1"/> <!--指定一张图片-->

按一下按钮换图片

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button
= (Button) findViewById(R.id.button);
imageView
= (ImageView) findViewById(R.id.image_view);
button.setOnClickListener(
this);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button:
imageView.setImageResource(R.drawable.img_2);
break;
default:
break;
}
}
}

5.ProgressBar(进度条)

<ProgressBar
android:id="@+id/progress_bar"
android:layout_width
="match_parent"
android:layout_height
="wrap_content" />

按一下按钮,改变进度条状态

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button
= (Button) findViewById(R.id.button);
progressBar
= (ProgressBar)findViewById(R.id.progress_bar);
button.setOnClickListener(
this);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button:
if(progressBar.getVisibility() == View.GONE){
progressBar.setVisibility(View.VISIBLE);
}
else{
progressBar.setVisibility(View.GONE);
}
break;
default:
break;
}
}
}

控件的可见属性(使用android:visibility指定):

visible:可见

invisible:不可见,但仍占据着屏幕的空间(透明)

gone:不可见,不占据屏幕空间

水平进度条

    <ProgressBar
android:id="@+id/progress_bar"
android:layout_width
="match_parent"
android:layout_height
="wrap_content"
style
="?android:attr/progressBarStyleHorizontal"
android:max
="100"/>

每按一次按钮,进度条加10

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button
= (Button) findViewById(R.id.button);
progressBar
= (ProgressBar)findViewById(R.id.progress_bar);
button.setOnClickListener(
this);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button:
int progress = progressBar.getProgress();
progress
= progress + 10;
progressBar.setProgress(progress);
break;
default:
break;
}
}
}

6.AlertDialog(弹出对话框)

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button
= (Button) findViewById(R.id.button);
progressBar
= (ProgressBar)findViewById(R.id.progress_bar);
button.setOnClickListener(
this);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button:
AlertDialog.Builder dialog
= new AlertDialog.Builder(MainActivity.this);
dialog.setTitle(
"This is Dialog"); //对话框标题
dialog.setMessage("Something important"); //对话框内容
dialog.setCancelable(false);//可否用back键关闭对话框
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { //设置确认按钮点击事件
@Override
public void onClick(DialogInterface dialog, int which) {

}
});
dialog.setNegativeButton(
"Cancel", new DialogInterface.OnClickListener() { //设置取消按钮点击事件
@Override
public void onClick(DialogInterface dialog, int which) {

}
});
dialog.show();
//显示对话框

break;
default:
break;
}
}
}

7.ProgressDialog(弹出带进度条的对话框)

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button
= (Button) findViewById(R.id.button);
progressBar
= (ProgressBar)findViewById(R.id.progress_bar);
button.setOnClickListener(
this);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button:
ProgressDialog progressDialog
= new ProgressDialog(MainActivity.this);
progressDialog.setTitle(
"This is a ProgressDialog");
progressDialog.setMessage(
"Loading...");
progressDialog.setCancelable(
true);
progressDialog.show();
break;
default:
break;
}
}
}