MainActivity.java
package com.kanxue.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_main_test1).setOnClickListener(this);
findViewById(R.id.btn_main_test4).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_main_test1:
startActivity(new Intent(this,SimpleConponentActivity.class));
break;
case R.id.btn_main_test4:
startActivity(new Intent(this,DialogActivity.class));
break;
default:
break;
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_main_test1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试常用简单的Component" />
<Button
android:id="@+id/btn_main_test2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试菜单Component" />
<Button
android:id="@+id/btn_main_test3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试进度条Component" />
<Button
android:id="@+id/btn_main_test4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试对话框Component" />
</LinearLayout>
DialogActivity.java
package com.kanxue.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class DialogActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
public void showAD(View view){
new AlertDialog.Builder(this).setTitle("删除数据").setMessage("您确定删除数据吗").setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this,"删除数据",Toast.LENGTH_LONG).show();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this,"取消删除数据",Toast.LENGTH_LONG).show();
}
}).show();
}
public void showLD(View view){
final String[] items={"红","蓝","绿","灰"};
new AlertDialog.Builder(this).setTitle("指定背景颜色").setSingleChoiceItems(items, 6, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this,items[which],Toast.LENGTH_LONG).show();
dialog.dismiss();
}
}).show();
}
public void showCD(View view){
View view1=View.inflate(this,R.layout.dialog_view,null);
final EditText edt_name=view1.findViewById(R.id.et_dialog_name);
final EditText edt_pwd=view1.findViewById(R.id.et_dialog_pwd);
new AlertDialog.Builder(this).setView(view1).setNegativeButton("取消",null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name=edt_name.getText().toString();
String pwd=edt_pwd.getText().toString();
Toast.makeText(DialogActivity.this,name+"----"+pwd,Toast.LENGTH_LONG).show();
}
}).show();
}
public void showPD(View view){
final ProgressDialog dialog=ProgressDialog.show(this,"数据加载","数据加载中…………");
new Thread(){
@Override
public void run() {
super.run();
for (int i = 0; i < 20; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
dialog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(DialogActivity.this,"加载完成",Toast.LENGTH_LONG).show();
}
});
}
}.start();
}
public void showPD2(View view){
final ProgressDialog pd= new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
new Thread(new Runnable() {
@Override
public void run() {
int count=100;
pd.setMax(count);
for (int i = 0; i < count; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
pd.setProgress(pd.getProgress()+1);
}
pd.dismiss();
}
}).start();
}
public void showDateAD(View view){
Calendar calendar=Calendar.getInstance();
int year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH);
int day=calendar.get(Calendar.DAY_OF_MONTH);
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Log.e("TAG",year+"--"+(month+1)+"--"+dayOfMonth);
}
},year,month,day).show();
}
public void showTimeAD(View view){
Calendar calendar=Calendar.getInstance();
int hour=calendar.get(Calendar.HOUR);
int minute=calendar.get(Calendar.MINUTE);
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Log.e("TAG",hourOfDay+"--"+minute);
}
},hour,minute,true).show();
}
}
activity_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- AlertDialog -->
<Button
android:id="@+id/btn_test4_ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showAD"
android:text="显示一般AlertDialog" />
<Button
android:id="@+id/btn_test4_ld"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showLD"
android:text="显示单选列表AlertDialog" />
<Button
android:id="@+id/btn_test4_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showCD"
android:text="显示自定义AlertDialog" />
<!-- ProgressDialog -->
<Button
android:id="@+id/btn_test4_pd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showPD"
android:text="显示圆形进度ProgressDialog"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/btn_test4_pd2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showPD2"
android:text="显示水平进度ProgressDialog" />
<!-- DatePickerDialog与TimePickerDialog -->
<Button
android:id="@+id/btn_test4_dd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showDateAD"
android:text="显示DatePickerDialog"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/btn_test4_td"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showTimeAD"
android:text="显示TimePickerDialog" />
</LinearLayout>
SimpleConponentActivity.java
package com.kanxue.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class SimpleConponentActivity extends AppCompatActivity {
private TextView tv_simple_message;
private EditText et_simple_number;
private Button btn_simple_submit;
private ImageView iv_simple_icon;
private CheckBox cb_simple_basket;
private CheckBox cb_simple_foot;
private CheckBox cb_simple_pingpang;
private RadioGroup rg_simple_sex;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_conponent);
tv_simple_message = findViewById(R.id.tv_simple_message);
tv_simple_message.setText("江科大");
et_simple_number = findViewById(R.id.et_simple_number);
btn_simple_submit = findViewById(R.id.btn_simple_submit);
btn_simple_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String number = et_simple_number.getText().toString();
Toast.makeText(SimpleConponentActivity.this, number, Toast.LENGTH_SHORT).show();
}
});
iv_simple_icon = findViewById(R.id.iv_simple_icon);
iv_simple_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv_simple_icon.setBackgroundResource(android.R.drawable.alert_light_frame);
iv_simple_icon.setImageResource(android.R.drawable.ic_media_pause);
}
});
cb_simple_basket = findViewById(R.id.cb_simple_basket);
cb_simple_foot = findViewById(R.id.cb_simple_foot);
cb_simple_pingpang = findViewById(R.id.cb_simple_pingpang);
cb_simple_foot.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Toast.makeText(SimpleConponentActivity.this, buttonView.getText().toString()+"足球被选中", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(SimpleConponentActivity.this, buttonView.getId()+"足球未被选中", Toast.LENGTH_SHORT).show();
}
}
});
rg_simple_sex = findViewById(R.id.rg_simple_sex);
rg_simple_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = findViewById(checkedId);
String sex = radioButton.getText().toString();
Toast.makeText(SimpleConponentActivity.this, sex, Toast.LENGTH_SHORT).show();
}
});
}
public void confirm(View view){
StringBuffer sb = new StringBuffer();
if(cb_simple_basket.isChecked()){
sb.append(cb_simple_basket.getText().toString()+",");
}
if(cb_simple_foot.isChecked()){
sb.append(cb_simple_foot.getText().toString()+",");
}
if(cb_simple_pingpang.isChecked()){
sb.append(cb_simple_pingpang.getText().toString()+",");
}
Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();
}
}
activity_simple_conponent.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_simple_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是TextView的内容"
android:background="#999999"
android:textColor="#ff0000"
android:textSize="20sp"/>
<EditText
android:id="@+id/et_simple_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入手机号"
android:inputType="phone">
</EditText>
<Button
android:id="@+id/btn_simple_submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="提交" />
<ImageView
android:id="@+id/iv_simple_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/alert_dark_frame"
android:src="@android:drawable/ic_menu_zoom"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好: " />
<CheckBox
android:id="@+id/cb_simple_basket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球" />
<CheckBox
android:id="@+id/cb_simple_foot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球" />
<CheckBox
android:id="@+id/cb_simple_pingpang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乒乓" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:onClick="confirm"/>
</LinearLayout>
<RadioGroup
android:id="@+id/rg_simple_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_simple_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/rb_simple_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:checked="true"/>
<RadioButton
android:id="@+id/rb_simple_nomale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="东方不败" />
</RadioGroup>
</LinearLayout>
dialog_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_background"
android:scaleType="fitXY"/>
<EditText
android:id="@+id/et_dialog_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名">
</EditText>
<EditText
android:id="@+id/et_dialog_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"/>
</LinearLayout>
|