<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="@android:color/holo_blue_bright"
android:textSize="20sp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:textAllCaps="false" //所有英文自动大写转换
/>
onClick()方法:
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
<EditText
android:id="@+id/edit_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something here"
android:maxLines="2"/>
public class Demo extends AppCompatActivity {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
Button button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.edit_id);
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(Demo.this, inputText, Toast.LENGTH_SHORT).show();
break;
default:
break;
} }
});
}}
点击按钮更换图片的方法:
Button button = (Button) findViewById(R.id.button);
final ImageView imageView = (ImageView) findViewById(R.id.image);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
imageView.setImageResource(R.drawable.image1);
break;
default:
break;
}
}
});
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="200"/>
可见属性 android:visiblity
可选值有三种:visible(可见) invisible(不可见 站空间) gone(不可见 不占空间)
一个点击事件: 可见/不可见
switch (v.getId())
case R.id.button:
if (progressBar.getVisibility() == View.GONE) {
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.GONE);
}
break;
default:
break;}
每点击一次按钮,就获得进度条的当前进度,然后在现有的进度上加10作为更新后的进度。
switch (v.getId()) {
case R.id.button:
int progress = progressBar.getProgress();
progress = progress + 10;
progressBar.setProgress(progress);
break;
default:
break;
}
一般用于提示一些非常重要的内容或者警告信息
switch (v.getId()) {
case R.id.button_alert:
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("This is a dialog");//对话框标题
dialog.setMessage("Something is 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;
}
}
switch (v.getId()) {
case R.id.button_alert:
ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);
progressDialog.setTitle("this is a progressDialog");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
break;
default:
break;
}
如果在setCancelable()方法中传入false,表示ProgressDialog是不能通过Back来取消掉的,这是就需要在代码中做好控制,当数据加载完成后必须调用ProgressDialog的dismiss()方法来关闭对话框,否则对话框将一直存在。