自己增加一些内容。不想存到代码片,在这里保存一下。
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is TextView"
android:gravity="center"
android:textSize="24sp"
android:textColor="#00ff00"
/>
<Button
android:id="@+id/button_show_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="显示文本"
/>
<EditText
android:id="@+id/input_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入文字"
android:maxLines="3"
/>
<Button
android:id="@+id/button_switch_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="切换图片"
/>
<ImageView
android:id="@+id/image_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/pmi"
/>
<Button
android:id="@+id/button_switch_progressbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示进度条"
/>
<Button
android:id="@+id/button_change_progess"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="更改进度"
/>
<!-- 默认是圆形进度条 -->
<!-- 下面用style语句设置成水平进度条 -->
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
/>
</LinearLayout>
MainActivity.java:
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button button;
private Button changeImg;
private Button switchVisible;
private Button changeProgress;
private EditText editText;
private ImageView imageView;
private ProgressBar progressBar;
private int[] img = new int[2];
private int imgIndex = 0;
{
img[0] = R.drawable.pmi;
img[1] = R.drawable.ic_launcher;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button_show_text);
changeImg = (Button) findViewById(R.id.button_switch_img);
switchVisible = (Button) findViewById(R.id.button_switch_progressbar);
changeProgress = (Button) findViewById(R.id.button_change_progess);
editText = (EditText) findViewById(R.id.input_edit_text);
imageView = (ImageView) findViewById(R.id.image_view);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
button.setOnClickListener(this);
changeImg.setOnClickListener(this);
switchVisible.setOnClickListener(this);
changeProgress.setOnClickListener(this);
changeButtonText(progressBar, switchVisible);
}
// 根据progressBar的状态设置按钮的文字
// OnCreate方法里只需要改文字,不用改进度条状态,故没有把改状态代码合并进来
private void changeButtonText(ProgressBar progressBar, Button button){
if (progressBar.getVisibility() == View.VISIBLE) {
button.setText("隐藏进度条");
} else {
button.setText("显示进度条");
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_show_text:
String text = editText.getText().toString();
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
break;
case R.id.button_switch_img:
// 轮换图片
imgIndex = (imgIndex + 1) % 2;
imageView.setImageResource(img[imgIndex]);
break;
case R.id.button_switch_progressbar:
if (progressBar.getVisibility() == View.VISIBLE) {
progressBar.setVisibility(View.INVISIBLE);
} else {
progressBar.setVisibility(View.VISIBLE);
}
changeButtonText(progressBar, switchVisible);
break;
case R.id.button_change_progess:
int progress = progressBar.getProgress();
progress += 10;
// 纯测试用,超过则清零
if (progress > progressBar.getMax()) {
progress -= progressBar.getMax();
}
progressBar.setProgress(progress);
break;
default:
break;
}
}
}
图片不上了,主要是记录一下简单的用法。