package com.syl.buttondemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextClock;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((Button)findViewById(R.id.btn1)).setOnClickListener(this);
((Button)findViewById(R.id.btn2)).setOnClickListener(this);
((Button)findViewById(R.id.btn3)).setOnClickListener(this);
}
@Override
public void onClick(View v){
TextView txt1 = (TextView)findViewById(R.id.txt1);
switch (v.getId()){
case R.id.btn1:{
txt1.setText("单击Button1");
}break;
case R.id.btn2:{
txt1.setText("单击Button2");
}break;
case R.id.btn3:{
txt1.setText("单击Button3");
}break;
}
}
}
package com.syl.buttondemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnLongClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnLongClickListener(this);
}
//长按
@Override
public boolean onLongClick(View v){
if(v.getId() == R.id.btn1){
Toast.makeText(MainActivity.this,"长按事件",Toast.LENGTH_SHORT).show();
//震动提示
Vibrator vib = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(500);//震动半秒
}
return true;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnLongClickListener(this);
}
package com.syl.buttondemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn2 = (Button)findViewById(R.id.btn2);
btn2.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event){
if(v.getId() == R.id.btn2){
TextView txt = (TextView) findViewById(R.id.txt1);
if(event.getAction()==MotionEvent.ACTION_DOWN){
txt.setText("手指按下");
}else if(event.getAction()==MotionEvent.ACTION_UP){
txt.setText("手指离开");
}else if(event.getAction()==MotionEvent.ACTION_MOVE){
txt.setText("手指滑动");
}
}
return true;
}
}
package com.syl.edittextdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText edit1 = (EditText)findViewById(R.id.edit1);
Button btn = (Button)findViewById(R.id.btn1);
btn.setText(edit1.getText());
}
});
}
}
package com.syl.alertdialogdemo;
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.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(this);
}
@Override
public void onClick(View v){
if(v.getId()==R.id.btn1){
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
dlg.setTitle("请确认您的操作");
dlg.setMessage("真得要这么做吗");
dlg.setCancelable(false);
//肯定操作按钮
dlg.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"确认操作",Toast.LENGTH_LONG).show();
}
});
//取消操作
dlg.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"取消操作",Toast.LENGTH_SHORT).show();
}
});
dlg.show();
}
}
}
在Activity中使用菜单,需要做以下几个步骤
package com.syl.menudemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//关联菜单
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.mainmenu,menu);
return true;
}
//响应菜单项
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuitem1:
{
Toast.makeText(this,"菜单项1",Toast.LENGTH_SHORT).show();
}break;
case R.id.menuitem2:
{
Toast.makeText(this,"菜单项2",Toast.LENGTH_SHORT).show();
}break;
case R.id.menuitem3:
{
Toast.makeText(this,"菜单项3",Toast.LENGTH_SHORT).show();
}break;
}
return true;
}
}
例二:嵌套菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-zuto">
<item
android:id="@+id/menu_settings"
android:title="设置"
android:icon="@mipmap/ic_launcher"
app:showAsAction="always"/>
<item
android:id="@+id/menu_check"
android:title="自动保存"
android:checkable="true"
android:checked="true"/>
<item
android:id="@+id/menu_help"
android:title="学号姓名">
<menu>
<item
android:id="@+id/xuehao"
android:title="学号"/>
<item
android:id="@+id/xingming"
android:title="姓名"/>
</menu>
</item>
<item
android:id="@+id/menu_exit"
android:title="退出"
app:showAsAction="never"
android:orderInCategory="100"/>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择性别!"/>
<RadioGroup
android:id="@+id/rg_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/sex_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保密"
android:checked="true"/>
<RadioButton
android:id="@+id/sex_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:id="@+id/sex_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1"/>
</LinearLayout>
package com.syl.radiobuttondemo;
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 rgsex = (RadioGroup)findViewById(R.id.rg_sex);
Button btn1 = (Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int rdoId= rgsex.getCheckedRadioButtonId();
String msg;
switch (rdoId){
case R.id.sex_1:msg="男";break;
case R.id.sex_2:msg="女";break;
default:msg="保密";break;
}
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/chk1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="经济"/>
<CheckBox
android:id="@+id/chk2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="汽车"/>
<CheckBox
android:id="@+id/chk3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐"/>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="关注的内容" />
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.syl.checkbox;
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.TextView;
public class MainActivity extends AppCompatActivity {
private int[] chkList = {
R.id.chk1,R.id.chk2,R.id.chk3};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 =(Button)findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = "";
for(int chkId: chkList){
CheckBox chk = (CheckBox)findViewById(chkId);
if(chk.isChecked()) msg = msg + "" + chk.getText();
}
TextView txt1 = (TextView)findViewById(R.id.txt1);
txt1.setText(msg);
}
});
}
}
注意:如需要在复选框在选中状态自动响应时,可以实现它的onCheckedChanged事件
出现问题,下次解决
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/robot"/>
</LinearLayout>
package com.syl.imageviewdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private int counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image = (ImageView)findViewById(R.id.img1);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView img = (ImageView)v;
if(counter % 2 ==0){
img.setImageResource(R.drawable.round);
counter++;
}else{
img.setImageResource(R.drawable.robot);
counter++;
}
}
});
}
}
9.1.1 使用ArrayAdapter泛型类创建一个数组适配器对象,用于向ListView组件绑定数据,其构造函数包括以下三个参数
9.1.2 代码最后,使用ListView对象的setAdapter()方法将Arraydapter对象绑定到ListView对象
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list1"/>
</LinearLayout>
package com.syl.listviewdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
//列表显示的数据
private String[] data = {
"aaa","bbb","ccc","ddd"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化列表
ArrayAdapter<String> ada = new ArrayAdapter<>(
MainActivity.this,
android.R.layout.simple_list_item_1,
data);
ListView list1 = (ListView)findViewById(R.id.list1);
list1.setAdapter(ada);
}
}
9.2.1 MainActivity类实现AdapterView.OnItemClickListener接口,并重写onItemClick()方法
。。。。。
package com.syl.progressbardemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ProgressBar pb1 = (ProgressBar)findViewById(R.id.pb1);
pb1.setProgress(0);
Button btn = (Button)findViewById(R.id.btn1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pb1.setProgress(0);
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
int cur = pb1.getProgress();
cur += 10;
if(cur <= 100){
pb1.setProgress(cur);
}else {
this.cancel();
}
}
};
timer.scheduleAtFixedRate(task,0,1000);
}
});
}
}
11.1 SeekBar组件中要设置最大值
11.2 在MainActivity.java文件中,实现SeekBar.OnSeekBarChangeListener接口,并重写以下三个方法
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:id="@+id/seekbar1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:max="100"/>
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.syl.seekbardemo;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
TextView txt1;
SeekBar seekbar1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekbar1 =(SeekBar)findViewById(R.id.seekbar1);
seekbar1.setOnSeekBarChangeListener(this);
txt1 = (TextView)findViewById(R.id.txt1);
}
@Override
public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser){
txt1.setText(String.format("当前值:%d",progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekbar1){
seekbar1.setBackgroundColor(Color.rgb(200,200,200));
}
@Override
public void onStopTrackingTouch(SeekBar seekbar1){
seekbar1.setBackgroundColor(Color.WHITE);
}
}
。。。。