2021-07-11

基本UI组件

文本框

文本框应用展示

2021-07-11

补充操作

2021-07-11

2021-07-11

编辑框

应用展示

2021-07-11

补充操作

2021-07-11

2021-07-11

在编辑框内绘制图像属性

2021-07-11

2021-07-11

实例

2021-07-11

2021-07-11

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="6"
        android:layout_margin="10dp"
        android:padding="5dp"
        android:gravity="top"
        android:background="#D6D6D6"
        android:textSize="20dp"
        android:hint="说点什么吧..."
        android:inputType="textMultiLine"/>      <!--多行-->
    <TextView
        android:drawableLeft="@mipmap/add"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center_vertical"
        android:textSize="20dp"
        android:layout_margin="10dp"
        android:padding="5dp"
        android:drawablePadding="8dp"
        android:text="添加图片"
        android:background="#D6D6D6"
        />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="221dp"
        android:layout_margin="10dp"
        android:scaleType="fitXY"
        android:src="@mipmap/img02" />
</LinearLayout>

2021-07-11

普通按钮

应用展示

2021-07-11

为普通按钮添加事件监听器

2021-07-11

通过匿名内部类作为单击事件监听器

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="button1" />

</RelativeLayout>
package com.example.common_button;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"单击了按钮1",Toast.LENGTH_LONG).show();
            }
        });
    }
}

通过onClick属性实现单击事件监听器

2021-07-11

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="button2"
        android:onClick="myClick"/>

</RelativeLayout>
package com.example.common_button;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"单击了按钮1",Toast.LENGTH_LONG).show();
            }
        });
    }

    public void myClick(View view){
        Toast.makeText(MainActivity.this,"单击了按钮2",Toast.LENGTH_LONG).show();
    }
}

2021-07-11

实例

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="button2"
        android:onClick="myClick"/>

</RelativeLayout>
package com.example.common_button;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        setContentView(R.layout.main_1);
        Button button = findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(MainActivity.this,"单击了按钮1",Toast.LENGTH_LONG).show();
                Toast.makeText(MainActivity.this,"登录...",Toast.LENGTH_LONG).show();
            }
        });
    }

    public void myClick(View view){
        Toast.makeText(MainActivity.this,"单击了按钮2",Toast.LENGTH_LONG).show();
    }
}

2021-07-11

图片按钮

图片按钮展示

2021-07-11

ImageButton和Button的区别

2021-07-11

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/player"
        android:background="#0000"      
        /> <!-- #0000是完全透明的颜色,可以取消图片的背景-->

</RelativeLayout>

效果图

2021-07-11

单选按钮

单选按钮展示

2021-07-11

单选实现

<?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:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">    <RadioGroup        android:id="@+id/radio_group"        android:layout_width="wrap_content"        android:layout_height="wrap_content">    <RadioButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="男"        android:textSize="20dp"        android:checked="true"        />                              <!--checked默认选择男-->        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="女"            android:textSize="20dp"            />        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="保密"            android:textSize="20dp"            />    </RadioGroup></LinearLayout>
package com.example.radiobutton;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                RadioButton radioButton = (RadioButton) findViewById(checkedId);                Toast.makeText(MainActivity.this,"您选择了"+radioButton.getText(),Toast.LENGTH_LONG).show();            }        });    }}

2021-07-11

2021-07-11

单选补充

<?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:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">    <RadioGroup        android:id="@+id/radio_group"        android:layout_width="wrap_content"        android:layout_height="wrap_content">    <RadioButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="男"        android:textSize="20dp"        />                              <!--checked默认选择男-->        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="女"            android:textSize="20dp"            />        <RadioButton            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="保密"            android:textSize="20dp"            />    </RadioGroup>    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="提交"        /></LinearLayout>
package com.example.radiobutton;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                RadioButton radioButton = (RadioButton) findViewById(checkedId);                Toast.makeText(MainActivity.this,"您选择了"+radioButton.getText(),Toast.LENGTH_LONG).show();            }        });        Button button = (Button)findViewById(R.id.button1);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                for(int i=0;i<radioGroup.getChildCount();i++){       //getChildCount获取RadioButton的个数                    RadioButton b = (RadioButton)radioGroup.getChildAt(i); //getChildAt(i)以每个RadioButton的位置获取对象                    if(b.isChecked()){                        Toast.makeText(MainActivity.this,b.getText(),Toast.LENGTH_LONG).show();                        break;                    }                }            }        });    }}

2021-07-11

实例

<?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: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:layout_margin="10dp"        android:text="  14.如图7所示,动圈式话筒的膜片与线圈固定在一起,线圈套在磁铁上。当我们对着话筒讲话时,声音使膜片振动,膜片带动线圈也一起振动,于是线圈中产生了随声音变化的电流。则下列电器的原理与动圈式话筒的原理相同的是"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintLeft_toLeftOf="parent"        app:layout_constraintRight_toRightOf="parent"        app:layout_constraintTop_toTopOf="parent" />    <RadioGroup        android:id="@+id/RadioGroup1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        >        <RadioButton            android:id="@+id/RadioButton1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="电动机"            />        <RadioButton            android:id="@+id/RadioButton2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="发电机"            />        <RadioButton            android:id="@+id/RadioButton3"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="电磁铁"            />        <RadioButton            android:id="@+id/RadioButton4"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="电铃"            />    </RadioGroup>    <Button        android:id="@+id/Button1"        android:layout_margin="50dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="提交"        /></LinearLayout>
package com.example.radiobutton_event;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.RadioGroup1);        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                RadioButton radioButton = (RadioButton)findViewById(checkedId);                //Toast.makeText(MainActivity.this,"您点击了"+radioButton.getText(),Toast.LENGTH_LONG).show();            }        });        Button button = (Button)findViewById(R.id.Button1);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                for(int i=0;i<radioGroup.getChildCount();i++){                    RadioButton b = (RadioButton)radioGroup.getChildAt(i);                    if(b.isChecked()){                        Toast.makeText(MainActivity.this,b.getText(),Toast.LENGTH_LONG).show();                        break;                    }                }            }        });    }}

2021-07-11

实例改进

package com.example.radiobutton_event;import androidx.appcompat.app.AlertDialog;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.RadioGroup1);        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                RadioButton radioButton = (RadioButton)findViewById(checkedId);                //Toast.makeText(MainActivity.this,"您点击了"+radioButton.getText(),Toast.LENGTH_LONG).show();            }        });        Button button = (Button)findViewById(R.id.Button1);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                for(int i=0;i<radioGroup.getChildCount();i++){                    RadioButton b = (RadioButton)radioGroup.getChildAt(i);                    if(b.isChecked()){                        if(b.getText().equals("电磁铁")){                            Toast.makeText(MainActivity.this,"回答正确",Toast.LENGTH_LONG).show();                        }else{                            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);                            builder.setMessage("回答错误请看解析");                            builder.setPositiveButton("确定",null).show();   //警告窗的确定按钮                        }                        break;                    }                }            }        });    }}

2021-07-11

2021-07-11

复选框

复选框应用

2021-07-11

<?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:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">    <CheckBox        android:id="@+id/Checkbox1"        android:layout_margin="10dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:text="体育"        />    <CheckBox        android:id="@+id/Checkbox2"        android:layout_margin="10dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="音乐"        />    <CheckBox        android:id="@+id/Checkbox3"        android:layout_margin="10dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="美术"        /></LinearLayout>
package com.example.checkbox;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.CheckBox;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        CheckBox checkBox1 = (CheckBox)findViewById(R.id.Checkbox1);        CheckBox checkBox2 = (CheckBox)findViewById(R.id.Checkbox2);        CheckBox checkBox3 = (CheckBox)findViewById(R.id.Checkbox3);        checkBox1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(MainActivity.this,checkBox1.getText(),Toast.LENGTH_LONG).show();            }        });        checkBox2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(MainActivity.this,checkBox2.getText(),Toast.LENGTH_LONG).show();            }        });        checkBox3.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(MainActivity.this,checkBox3.getText(),Toast.LENGTH_LONG).show();            }        });    }}

2021-07-11

实例改进

<?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:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">    <ImageView        android:layout_gravity="top"        android:layout_width="match_parent"        android:layout_height="200dp"        android:src="@mipmap/authorize"        />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="登陆后该应用将获得以下权限"        android:textSize="20dp"        android:layout_marginLeft="20dp"        />    <CheckBox        android:id="@+id/checkbox1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="获得你的公开信息(昵称、头像等)"        android:textSize="15dp"        android:layout_marginLeft="23dp"        />    <CheckBox        android:id="@+id/checkbox2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="寻找与你共同使用该应用的好友"        android:textSize="15dp"        android:layout_marginLeft="23dp"        />    <CheckBox        android:id="@+id/checkbox3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="帮助你通过该应用向好友发送信息"        android:textSize="15dp"        android:layout_marginLeft="23dp"        />    <Button        android:id="@+id/sin_in"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="确认登录"        android:textSize="20dp"        android:layout_marginLeft="20dp"        android:layout_marginRight="20dp"        android:background="@color/green_01"        />    <Button        android:id="@+id/sin_out"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="取消"        android:textSize="20dp"        android:layout_marginLeft="20dp"        android:layout_marginRight="20dp"        android:layout_marginTop="20dp"        android:background="#FFFFFF"        /></LinearLayout>
package com.example.check_event;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    String checked = " ";     //利用checked来增加授权的信息    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        CheckBox checkBox1 = (CheckBox)findViewById(R.id.checkbox1);        CheckBox checkBox2 = (CheckBox)findViewById(R.id.checkbox2);        CheckBox checkBox3 = (CheckBox)findViewById(R.id.checkbox3);        checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                checked += checkBox1.getText().toString();                Toast.makeText(MainActivity.this,checked,Toast.LENGTH_SHORT).show();            }        });        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                checked += checkBox2.getText().toString();                Toast.makeText(MainActivity.this,checked,Toast.LENGTH_SHORT).show();            }        });        checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                checked += checkBox3.getText().toString();                Toast.makeText(MainActivity.this,checked,Toast.LENGTH_SHORT).show();            }        });        Button button = (Button)findViewById(R.id.sin_in);        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(MainActivity.this,"登录...",Toast.LENGTH_SHORT).show();            }        });    }}

2021-07-11

日期选择器

应用展示

2021-07-11

<?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:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <DatePicker        android:id="@+id/Datapicker"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="top"        android:layout_margin="20dp"        android:padding="20dp"        /></LinearLayout>
package com.example.datesticker;import androidx.appcompat.app.AppCompatActivity;import android.app.Activity;import android.os.Bundle;import android.widget.DatePicker;import android.widget.Toast;import java.util.Calendar;import java.util.Date;public class MainActivity extends Activity {    int year,month,day;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        DatePicker datePicker = (DatePicker) findViewById(R.id.Datapicker);        Calendar calendar = Calendar.getInstance();  //实例化日历        year = calendar.get(Calendar.YEAR);        month = calendar.get(Calendar.MONTH);        day = calendar.get(Calendar.DAY_OF_MONTH);        datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {  //用本地的时间初始化日期选择器            @Override            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {                MainActivity.this.year = year;                MainActivity.this.month = monthOfYear;                MainActivity.this.day = dayOfMonth;   //选择的是日期选择器里面的日期                show(year,monthOfYear,dayOfMonth);    //显示的是日期选择器里面的日期            }        });    }    private void show(int year,int month,int day){        String str = year+"年"+(month + 1)+"月"+day+"日";        Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();    }}

2021-07-11

时间选择器

应用展示

2021-07-11

<?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:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">    <TimePicker        android:id="@+id/Timepeicker"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        /></LinearLayout>
package com.example.timesticker;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.widget.TimePicker;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TimePicker timePicker = (TimePicker) findViewById(R.id.Timepeicker);        timePicker.setIs24HourView(true);  //设置为24小时格式        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {            @Override            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {                String str = hourOfDay +"时"+ minute+"分";                Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();            }        });    }}

2021-07-11

2021-07-11

计时器

应用展示

2021-07-11

计时器的方法

2021-07-11

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <Chronometer        android:id="@+id/chronometer_"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:format="%s"        />  </RelativeLayout>
package com.example.chronometer;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.os.SystemClock;import android.widget.Chronometer;import java.sql.Clob;import java.time.Clock;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer_);        chronometer.setBase(SystemClock.elapsedRealtime());            //设置计时器开始时间        chronometer.setFormat("%s");        chronometer.start();        chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {            @Override            public void onChronometerTick(Chronometer chronometer) {                if(SystemClock.elapsedRealtime()-chronometer.getBase()>=60000){                    chronometer.stop();                }            }        });    }}

2021-07-11

上一篇:Android 了解Activity


下一篇:Android 利用正则表达式获取检验手机号