本次学习知识点:
- TextView -文本
- Button -按钮
- EditText -文本编辑框
- ImageView -图片
- ProgressBar -进度条
- AlterDialog -对话框
直接贴相关代码,会比较凌乱,仅作记录使用。
(创建空项目的基础下)
- MainActivity.java
package com.example.uiwidgettest;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private ImageView imageView;
private ProgressBar progressBar;
private int x=1;
@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.image_view);
progressBar=(ProgressBar)findViewById(R.id.progress_bar);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
// String inputText=editText.getText().toString();
// Toast.makeText(MainActivity.this, inputText, Toast.LENGTH_SHORT).show();
// if (x==1){
// imageView.setImageResource(R.drawable.img_2);
// x=2;
// }
// else{
// imageView.setImageResource(R.drawable.img_1);
// x=1;
// }
// int progress=progressBar.getProgress();
// progress+=10;
// progressBar.setProgress(progress);
// if (progressBar.getVisibility()==View.GONE){
// progressBar.setVisibility(View.VISIBLE);
// }else{
// progressBar.setVisibility(View.GONE);
// }
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("This is Dialog.");
dialog.setMessage("Something important.");
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();
}
});
}
}
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24sp"
android:textColor="#00ff00"
android:text="This is TextView"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:textAllCaps="false"/>
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something here"
android:maxLines="2"
/>
<!--<ImageView-->
<!--android:id="@+id/image_view"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:src="@drawable/img_1"/>-->
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"/>
</LinearLayout>