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();