android常用控件的使用方法

简介:

引言

xml很强大

TextView

<TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24sp"
        android:textColor="#00ff00"
        android:text="This is textview"
         />

Button

语法

<!-- textAllCaps避免全部变为大写 -->
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24sp"
        android:textColor="#00ff00"
        android:text="This is textview"
        android:textAllCaps="false"
         />

添加点击处理

1.第一种

package com.example.diandodo.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; // 引入可以使用日志
import android.view.View;
import android.widget.Button;

public class HelloWorldActivity extends AppCompatActivity {
    private static final String TAG = "HelloWorldActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hello_world_layout);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"Click");
            }
        });
    }
}

2.第二种

package com.example.diandodo.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; // 引入可以使用日志
import android.view.View;
import android.widget.Button;

public class HelloWorldActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "HelloWorldActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hello_world_layout);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                Log.d(TAG,"Click");
                break;
        }

    }
}

EditText

允许用户输入和编辑内容

<EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:hint="Type something here"
         />

小案例,点击获取内容

1.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- textAllCaps避免全部变为大写 -->
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="点击获取"
        android:textAllCaps="false"
        />

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:hint="Type something here"
        android:maxLines="2"
         />

</LinearLayout>

2.业务

package com.example.diandodo.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; // 引入可以使用日志
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class HelloWorldActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "HelloWorldActivity";
    private EditText editText; // 定义一个全局变量
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hello_world_layout);
        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(HelloWorldActivity.this,inputText,Toast.LENGTH_SHORT).show();
                break;
        }

    }
}

ImageView

图片展示的控件。

图片通常放在以“drawable”开头的目录下。

<ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/img_1"
        />

小案例,点击更改图片

1.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- textAllCaps避免全部变为大写 -->
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="点击获取"
        android:textAllCaps="false"
        />

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:hint="Type something here"
        android:maxLines="2"
         />

    <Button
        android:id="@+id/button_change_img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="点击改变图片"
        android:textAllCaps="false"
        />

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/img_1"
        />

</LinearLayout>

2.业务

package com.example.diandodo.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; // 引入可以使用日志
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class HelloWorldActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "HelloWorldActivity";
    private EditText editText; // 定义一个全局变量
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hello_world_layout);
        editText = (EditText) findViewById(R.id.edit_text);
        imageView= (ImageView) findViewById(R.id.image_view);
        Button button = (Button) findViewById(R.id.button);
        Button button_change_img = (Button) findViewById(R.id.button_change_img);
        button.setOnClickListener(this);
        button_change_img.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                String inputText = editText.getText().toString();
                Toast.makeText(HelloWorldActivity.this,inputText,Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_change_img:
                imageView.setImageResource(R.drawable.img_2);
                break;
        }

    }
}

ProgressBar

进度条

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

小案例,点击隐藏显示进度条

1.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- textAllCaps避免全部变为大写 -->
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="点击获取"
        android:textAllCaps="false"
        />

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:hint="Type something here"
        android:maxLines="2"
         />

    <Button
        android:id="@+id/button_change_img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="点击改变图片"
        android:textAllCaps="false"
        />

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/img_1"
        />

    <Button
        android:id="@+id/button_set_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="点击显示隐藏"
        android:textAllCaps="false"
        />

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


</LinearLayout>

2.业务

package com.example.diandodo.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; // 引入可以使用日志
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class HelloWorldActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "HelloWorldActivity";
    private EditText editText; // 定义一个全局变量
    private ImageView imageView;
    private ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hello_world_layout);
        editText = (EditText) findViewById(R.id.edit_text);
        imageView= (ImageView) findViewById(R.id.image_view);
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        Button button = (Button) findViewById(R.id.button);
        Button button_change_img = (Button) findViewById(R.id.button_change_img);
        Button button_set_view = (Button) findViewById(R.id.button_set_view);

        button.setOnClickListener(this);
        button_change_img.setOnClickListener(this);
        button_set_view.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                String inputText = editText.getText().toString();
                Toast.makeText(HelloWorldActivity.this,inputText,Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_change_img:
                imageView.setImageResource(R.drawable.img_2);
                break;

            case R.id.button_set_view:
                if (progressBar.getVisibility() == View.GONE) {
                    progressBar.setVisibility(View.VISIBLE); // INVISIBLE
                } else {
                    progressBar.setVisibility(View.GONE);
                }
                break;
        }

    }
}

设为长的进度条

1.布局调整

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

2.业务处理

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                String inputText = editText.getText().toString();
                Toast.makeText(HelloWorldActivity.this,inputText,Toast.LENGTH_SHORT).show();
                break;
            case R.id.button_change_img:
                imageView.setImageResource(R.drawable.img_2);
                break;

            case R.id.button_set_view:
//                if (progressBar.getVisibility() == View.GONE) {
//                    progressBar.setVisibility(View.VISIBLE); // INVISIBLE
//                } else {
//                    progressBar.setVisibility(View.GONE);
//                }
                int progress = progressBar.getProgress();
                progress = progress + 10;
                progressBar.setProgress(progress);
                break;
        }

    }

这样每次点击,都会增加进度。

AlertDialog

弹出对话框,置顶于所有界面元素之上。

            AlertDialog.Builder dialog = new AlertDialog.Builder(HelloWorldActivity.this);
                dialog.setTitle("这是Dialog");
                dialog.setMessage("一些重要的事情");
                dialog.setCancelable(false);
                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();

ProgressDialog

会在对话框中显示一个进度条,一般用于表示当前操作比较耗时,让用户耐心等待。

ProgressDialog progressDialog = new ProgressDialog(HelloWorldActivity.this);
                progressDialog.setTitle("这是progressDialog");
                progressDialog.setMessage("Loading...");
                progressDialog.setCancelable(true);
                progressDialog.show();



本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/7499174.html,如需转载请自行联系原作者
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
40 1
|
4月前
|
Java Android开发
Android Studio入门之按钮触控的解析及实战(附源码 超详细必看)(包括按钮控件、点击和长按事件、禁用与恢复按钮)
Android Studio入门之按钮触控的解析及实战(附源码 超详细必看)(包括按钮控件、点击和长按事件、禁用与恢复按钮)
160 0
|
6月前
|
存储 传感器 定位技术
《移动互联网技术》 第四章 移动应用开发: Android Studio开发环境的使用方法:建立工程,编写源程序,编译链接,安装模拟器,通过模拟器运行和调试程序
《移动互联网技术》 第四章 移动应用开发: Android Studio开发环境的使用方法:建立工程,编写源程序,编译链接,安装模拟器,通过模拟器运行和调试程序
64 0
|
1月前
|
Android开发
[Android]RadioButton控件
[Android]RadioButton控件
12 0
|
3月前
|
Android开发
分享88个Android控件源代码总有一个是你想要的
分享88个Android控件源代码总有一个是你想要的
22 0
|
3月前
|
Android开发
分享89个Android控件源代码总有一个是你想要的
分享89个Android控件源代码总有一个是你想要的
71 0
|
4月前
|
XML Android开发 数据格式
[Android]开关控件Switch
[Android]开关控件Switch
35 0
|
4月前
|
Android开发 Kotlin
android开发,使用kotlin学习滚动控件RecyclerView
android开发,使用kotlin学习滚动控件RecyclerView
36 0
|
4月前
|
XML Java Android开发
Android Studio App开发中高级控件下拉列表Spinner的讲解及实战(附源码 超详细必看)
Android Studio App开发中高级控件下拉列表Spinner的讲解及实战(附源码 超详细必看)
72 0
|
8月前
|
缓存 Java API
#7,Android开发 控件 ProgressBar 进度条
#7,Android开发 控件 ProgressBar 进度条