过完年了,今年真的是水逆开局。。。
常用控件的使用(遇见一个更新一个)
- 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="#00fff2" //文字的颜色
android:text="This is TextView!" //文本内容
/>
android:gravity 用于指定文字的对齐方式,有top,bottom,left,right,center 等,可以用“ | ”来同时指定多个值,此处指定的 center 等同于 center_vertical | center_horizontal ,表示文字在水平和垂直方向都居中对齐。
- Button
非常熟悉的Button
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button"
android:textAllCaps="false"
/>
android:textAllCaps 用于决定是否将Button中所有的的英文字母进行大写转换。
- EditText
用户输入和编辑内容的地方
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请随便写点什么" //提示文本,用户点击输入就消失
android:maxLines="2" //最大行数,输入内容超过两行就向上滚动,而不是继续横向拉伸
/>
![手机运行效果](https://i-blog.csdnimg.cn/blog_migrate/91f6a8ca7f9f21f8005504a3498b96e5.jpeg)
![手机运行效果](https://i-blog.csdnimg.cn/blog_migrate/a4eeafd5be61599b66fc14b943d1bbbb.jpeg)
#结合button与editText
public class MainActivity extends AppCompatActivity {
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(new View.OnClickListener() {
@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();
break;
default:
break;
}
}
});
}
}
![手机运行效果](https://i-blog.csdnimg.cn/blog_migrate/36ca65c2743eb67285173c0940c6abf8.jpeg)
- imageView
图片的展示
<ImageView
android:id="@+id/img_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/img_1"
/>
![手机运行效果](https://i-blog.csdnimg.cn/blog_migrate/42b02bbe855d62ce43a161e4901b4683.jpeg)
通过代码更换图片:
public class MainActivity extends AppCompatActivity {
private EditText editText;
private ImageView imageView;
private ProgressBar progressBar;
@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);
imageView = (ImageView)findViewById(R.id.img_view);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
imageView.setImageResource(R.drawable.img_2);
break;
default:
break;
}
}
});
![手机运行效果](https://i-blog.csdnimg.cn/blog_migrate/42b02bbe855d62ce43a161e4901b4683.jpeg)
![手机运行效果](https://i-blog.csdnimg.cn/blog_migrate/abe50bff9777bc0e2142ca5f2f879e2b.jpeg)
-
ProgressBar
最讨厌的进度条
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
![手机运行效果](https://i-blog.csdnimg.cn/blog_migrate/bea1af49eac157ecec5909a114bc9a72.jpeg)
控制进度条的显示与消失
public class MainActivity extends AppCompatActivity {
private EditText editText;
private ImageView imageView;
private ProgressBar progressBar;
@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);
imageView = (ImageView)findViewById(R.id.img_view);
progressBar = (ProgressBar)findViewById(R.id.progress_bar);
button.setOnClickListener(new View.OnClickListener() {
@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 来控制Android控件的可见属性:
visible :表示控件可见(不指定android:visibility也代表控件可见)
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"
/>
-
AlertDialog
操作提示框
public class MainActivity extends AppCompatActivity {
private EditText editText;
private ImageView imageView;
private ProgressBar progressBar;
@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);
imageView = (ImageView)findViewById(R.id.img_view);
progressBar = (ProgressBar)findViewById(R.id.progress_bar);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("警告!");
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();
break;
default:
break;
}
}
});
}
}
![手机运行效果](https://i-blog.csdnimg.cn/blog_migrate/465433320e5badb1256b50e63cb99014.jpeg)
-
ProgressDialog
用户请耐心等待!
public class MainActivity extends AppCompatActivity {
private EditText editText;
private ImageView imageView;
private ProgressBar progressBar;
@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);
imageView = (ImageView)findViewById(R.id.img_view);
progressBar = (ProgressBar)findViewById(R.id.progress_bar);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("用户请耐心等待");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
break;
default:
break;
}
}
});
}
}
![手机运行效果](https://i-blog.csdnimg.cn/blog_migrate/b7651840f2815e99afb4b2e6a73aefc1.jpeg)
小结:部分控件的了解,EditText,Button,ImageView,ProgressBar,AlertDialog,ProgressDialog,
还需要在做demo时慢慢掌握,后续遇见新的控件会加入进来。