*TextView主要用于在界面上显示一段文本信息
*Button是按钮,是用户与程序交互的重要控件
*EditText是允许用户在控件里输入和编辑内容,在qq中输入账号密码时就会用到
*ImageView是在界面上展示图片的一个控件,提前准备好图片,图片复制粘贴在drawable中即可
*ProgressBar用于在界面上显示一个进度条,表示我们的程序在加载一些数据
在activity_main.xml中代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is TextView"
android:layout_gravity="center"
android:textColor="#397ae4"
android:textSize="24sp"
android:id="@+id/text_view" />
<Button
android:id="@+id/button"
android:text="显示EditText内容"
android:textAllCaps="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:text="图片切换"
android:textAllCaps="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button2"
android:text="进度条消失"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button3"
android:text="对话框"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edit_text"
android:hint="Type something"
android:maxLines="2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image_view"
android:src="@drawable/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
在MainActivity中代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageView imageView;
private EditText editText;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
editText = (EditText) findViewById(R.id.edit_text);
imageView =(ImageView) findViewById(R.id.image_view);
progressBar =(ProgressBar) findViewById(R.id.progress_bar);
button.setOnClickListener(this);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.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;//将EditText中内容以Toast形式显示出来
case R.id.button1:
imageView.setImageResource(R.drawable.b);//图片转换
break;
case R.id.button2:
if(progressBar.getVisibility()==View.GONE) {
//安卓的控件可选值有visible,invisible,gone。visible表明可见
progressBar.setVisibility(View.VISIBLE); }
//invibility表明不可见但是占据位置,gone表明不可见且不再占用任何屏幕空间
else {
progressBar.setVisibility(View.GONE);
}
break;
case R.id.button3:
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("this is a Dialog");
dialog.setMessage("something 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;
}
}
}
解析见注释
运行前
运行后