3.2 常用控件的使用方法
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="#000000"
android:text="Hello World!" />
Button
用于和用户进行交互,最后一项是使按钮名不再默认大写
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button01"
android:textAllCaps="false" />
为Button的点击事件注册一个监听器
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
}
});
}
}
EditText
允许用户在控件里输入和编辑内容
- hint显示一段提示性文本,当输入任何内容时,这段文本自动消失
- maxline指定了EditText的最大行数为两行,当输入的内容超过两行时,文本就会向上滚动,而EditText则不会继续拉伸
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something here"
android:maxLines="2" />
EditText与Button结合
点击按钮来获取EditText中输入的内容:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_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(MainActivity.this, inputText, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
ImageView
用于在界面上展示图片
在res目录下新建一个drawable-xhdpi目录,然后将事先准备好的两张图片img_1.png和imag_2.png复制到该目录下
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/img_1" />
通过代码动态地更改(点击按钮切换图片)ImageView中的图片:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//private EditText editText;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Button button = (Button) findViewById(R.id.button);
//editText=(EditText)findViewById(R.id.edit_text);
imageView=(ImageView)findViewById(R.id.image_view);
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();
imageView.setImageResource(R.drawable.img_2);
break;
default:
break;
}
}
}
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 EditText editText;
private ImageView imageView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Button button = (Button) findViewById(R.id.button);
//editText=(EditText)findViewById(R.id.edit_text);
imageView=(ImageView)findViewById(R.id.image_view);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
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();
imageView.setImageResource(R.drawable.img_2);
if (progressBar.getVisibility() == View.GONE) {
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.GONE);
}
break;
default:
break;
}
}
}
水平进度条:最大值为100
<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 EditText editText;
private ImageView imageView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Button button = (Button) findViewById(R.id.button);
//editText=(EditText)findViewById(R.id.edit_text);
imageView=(ImageView)findViewById(R.id.image_view);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
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();
imageView.setImageResource(R.drawable.img_2);
int progress = progressBar.getProgress();
progress=progress+10;
progressBar.setProgress(progress);
break;
default:
break;
}
}
}
AlertDialog
在当前的界面弹出一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽掉其他控件的交互能力,用于提示一些非常重要的内容或者警告信息
点击按钮就会弹出窗口
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//private EditText editText;
private ImageView imageView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Button button = (Button) findViewById(R.id.button);
//editText=(EditText)findViewById(R.id.edit_text);
imageView=(ImageView)findViewById(R.id.image_view);
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("Someting important.");
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;
}
}
}
ProgressDialog
和AlertDialog类似,可以在界面上弹出一个对话框,能够屏蔽掉其他控件的交互能力,但会在对话框中显示一个进度条,一般用于表示当前操作比较耗时,让用户耐心等待。
如果在setCancelable()中传入了false,表示ProgressDialog是不能通过back键取消掉的,当数据加载完成后可以调用ProgressDialog的dismiss()方法来关闭对话框,否则ProgressDialog将会一直存在。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//private EditText editText;
private ImageView imageView;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Button button = (Button) findViewById(R.id.button);
//editText=(EditText)findViewById(R.id.edit_text);
imageView=(ImageView)findViewById(R.id.image_view);
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 ProgressDialog");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
break;
default:
break;
}
}
}
以下用到哪些控件?
本文地址:https://blog.csdn.net/Black_Cat_33/article/details/114286153
上一篇: Python批量获取基金数据的方法步骤
下一篇: 你这是在玩我吗