加法
<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:id="@+id/et_num1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入第一个整数"/>
<EditText android:id="@+id/et_num2" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入第二个整数"/>
<Button android:id="@+id/btn_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="计算"/>
<TextView android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="结果:"/>
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText etNum1;
private EditText etNum2;
private Button btnAdd;
private TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etNum1 = findViewById(R.id.et_num1);
etNum2 = findViewById(R.id.et_num2);
btnAdd = findViewById(R.id.btn_add);
tvResult = findViewById(R.id.tv_result);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num1 = Integer.parseInt(etNum1.getText().toString());
int num2 = Integer.parseInt(etNum2.getText().toString());
int result = num1 + num2;
tvResult.setText("结果:" + result);
}
});
}
}
绿豆通讯录
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" android:orientation="vertical" android:padding="16dp">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="130dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓 名 :" android:textSize="18sp"/>
<EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入姓名" android:textSize="16sp"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电 话 :" android:textSize="18sp"/>
<EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入手机号码" android:textSize="16sp"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/btn_add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="2dp" android:layout_weight="1" android:background="#B9B9FF" android:text="添加" android:textSize="18sp"/>
<Button android:id="@+id/btn_query" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="2dp" android:layout_weight="1" android:background="#DCB5FF" android:text="查询" android:textSize="18sp"/>
<Button android:id="@+id/btn_update" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="2dp" android:layout_weight="1" android:background="#E6CAFF" android:text="修改" android:textSize="18sp"/>
<Button android:id="@+id/btn_delete" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="#ACD6FF" android:text="删除" android:textSize="18sp"/>
</LinearLayout>
<TextView android:id="@+id/tv_show" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:textSize="20sp"/>
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mEtName, mEtPhone;
private TextView mTvShow;
private Button mBtnAdd, mBtnQuery, mBtnUpdate, mBtnDelete;
private SQLiteDatabase db;
private MyHelper myHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper = new MyHelper(this);
db = myHelper.getWritableDatabase();
init();
}
private void init() {
mEtName = findViewById(R.id.et_name);
mEtPhone = findViewById(R.id.et_phone);
mTvShow = findViewById(R.id.tv_show);
mBtnAdd = findViewById(R.id.btn_add);
mBtnQuery = findViewById(R.id.btn_query);
mBtnUpdate = findViewById(R.id.btn_update);
mBtnDelete = findViewById(R.id.btn_delete);
mBtnAdd.setOnClickListener(this);
mBtnQuery.setOnClickListener(this);
mBtnUpdate.setOnClickListener(this);
mBtnDelete.setOnClickListener(this);
}
class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context, "itcast.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20), phone VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
private void query() {
Cursor cursor = db.query("information", null, null, null, null,
null, null);
mTvShow.setText("");//清空界面上显示的姓名与手机号码信息
if (cursor.getCount() == 0) {
Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
} else {
while (cursor.moveToNext()) {
mTvShow.append("\n" + "Name : " + cursor.getString(1) +
" ;Tel : " + cursor.getString(2));
}
}
cursor.close();
}
@Override
public void onClick(View v) {
String name, phone;
ContentValues values;
switch (v.getId()) {
case R.id.btn_add: //添加数据
name = mEtName.getText().toString();
phone = mEtPhone.getText().toString();
values = new ContentValues();//创建ContentValues对象
values.put("name", name); //将数据添加到ContentValues对象
values.put("phone", phone);
long id = db.insert("information", null, values);
if (id != -1) {
Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
query();//信息添加成功后调用query()方法将添加信息显示到界面上
} else {
Toast.makeText(this, "信息添加失败", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_query: //查询数据
query();
break;
case R.id.btn_update: //修改数据
Cursor cursor1 = db.query("information", null, null, null, null,
null, null);
if (cursor1.getCount() == 0) {
Toast.makeText(this, "没有数据可修改", Toast.LENGTH_SHORT).show();
} else {
values = new ContentValues();
values.put("phone", phone = mEtPhone.getText().toString());
int rows = db.update("information", values, "name=?",
new String[]{mEtName.getText().toString()}); //更新数据
if (rows > 0) {
Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
query();
} else {
Toast.makeText(this, "信息修改失败",
Toast.LENGTH_SHORT).show();
}
}
break;
case R.id.btn_delete: //删除数据
Cursor cursor2 = db.query("information", null, null, null, null,
null, null);
if (cursor2.getCount() == 0) {
Toast.makeText(this, "没有数据可删除", Toast.LENGTH_SHORT).show();
} else {
int delRows = db.delete("information", null, null);
if (delRows > 0) {
Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
mTvShow.setText("");
} else {
Toast.makeText(this, "信息删除失败",
Toast.LENGTH_SHORT).show();
}
}
break;
}
}
}
数据回传
manifests
<activity android:name=".SecondActivity" >
</activity>
<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:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="23dp" android:gravity="center" android:text="数据的传递与回传!" android:textSize="25dp"/>
<Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginTop="46dp" android:text="跳转到第二个页面" android:textSize="30dp"/>
<TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="23dp" android:text="回传数据:" android:textSize="25dp"/>
<TextView android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textSize="25dp"/>
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv3 = findViewById(R.id.text3);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivityForResult(intent,1);
}
});
}
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == 2) {
String data1 = data.getStringExtra("data");
tv3.setText(data1);
}
}
}
activity_main2.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:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="23dp" android:gravity="center" android:text="这里是第二个页面请输入内容!" android:textSize="25dp"/>
<EditText android:id="@+id/data1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:textSize="30dp" android:layout_marginTop="23dp" android:inputType="textPersonName" android:text="Name"/>
<Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginTop="10dp" android:text="返回第一个页面,并把数据回传" android:textSize="20dp"/>
</LinearLayout>
package com.example.myapplication;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.view.View;
import android.widget.Button;
public class MainActivity2 extends AppCompatActivity {
EditText tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv = findViewById(R.id.data1);
Button button1 = findViewById(R.id.button);
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
String data1 =tv.getText().toString();
Intent intent = new Intent();
intent.putExtra("data",data1);
setResult(2,intent);
finish();
}
});
}
}
无序广播
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送广播" android:layout_centerInParent="true"/>
</RelativeLayout>
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
String action = "cn.itcast.sendbroadcast";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(action);
sendBroadcast(intent);
}
});
MyBroadcastReceiver receiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter(action);
registerReceiver(receiver,intentFilter); //注册广播
}
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(MainActivity.this,"接收到无序广播",Toast.LENGTH_SHORT).show();
}
}
}
倒计时
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取验证码" android:layout_centerInParent="true"/>
</RelativeLayout>
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button button;
private boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (flag){
Intent intent = new Intent(MainActivity.this,TimeService.class);
startService(intent);
button.setText("倒计时60秒");
}
}
});
MyBroadcastReceiver receiver = new MyBroadcastReceiver(); //实例化广播接收者
String action = "com.example.answer_time";
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(action);
registerReceiver(receiver,intentFilter); //注册广播
}
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
int time = intent.getIntExtra("time",60);
if(time < 60 && time >0){
flag = false;
}else{
flag = true;
}
button.setText("倒计时"+time+"秒");
Log.e("yanwenhua","shengyu "+time);
}
}
}
TimeService.java
package com.example.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import androidx.annotation.Nullable;
import java.util.Timer;
import java.util.TimerTask;
public class TimeService extends Service {
int time = 60;
private Timer timer;
private TimerTask task;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
timer = new Timer(); //创建计时器对象
task = new TimerTask() {
@Override
public void run() {
time = time - 1;
Intent intent = new Intent();
intent.setAction("com.example.answer_time");
intent.putExtra("time",time);
sendBroadcast(intent);
if(time <=0){
stopSelf();
}
}
};
timer.schedule(task,0,1000);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
timer.cancel();
task = null;
timer = null;
}
}
Json
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
AssetManager assetManager = getAssets(); //获得assets资源管理器(assets中的文件无法直接访问,可以使用AssetManager访问)
InputStreamReader inputStreamReader = new InputStreamReader(assetManager.open("Dawn.json"), "UTF-8"); //使用IO流读取json文件内容
BufferedReader br = new BufferedReader(inputStreamReader);//使用字符高效流
String line;
StringBuilder builder = new StringBuilder();
while ((line = br.readLine()) != null) {
builder.append(line);}
br.close();
inputStreamReader.close();
JSONObject testJson = new JSONObject(builder.toString()); // 从builder中读取了json中的数据。
// 直接传入JSONObject来构造一个实例
JSONArray array = testJson.getJSONArray("banks");
Log.e("banks", array.toString());
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
String text = jsonObject.getString("text");
String value = jsonObject.getString("value");
Log.e("tag", "initData: " + text + value);
}} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();}}}
Dawn.json
{"banks" : [
{"value": "fby", "text":"12345678"},
{"value": "fby", "text":"2107381124"},
{"value": "fby", "text": "数据科学212"},
{"value": "f", "text": "android"},
{"value": "fff", "text": "studio"}]}
开饭了(广播)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white">
<RelativeLayout android:id="@+id/ll_horn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp">
<ImageView android:id="@+id/iv_horn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/horn"/>
<TextView android:id="@+id/tv_right_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/iv_horn" android:background="@drawable/content_right_bg" android:gravity="center" android:text="点击喇叭" android:textColor="@android:color/white"/>
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_right_content" android:layout_marginTop="20dp" android:layout_toRightOf="@id/iv_horn" android:src="@drawable/foods"/>
</RelativeLayout>
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/ll_horn" android:layout_marginTop="100dp">
<ImageView android:id="@+id/iv_rabbit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:src="@drawable/rabbit"/>
<TextView android:id="@+id/tv_left_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/iv_rabbit" android:background="@drawable/content_left_bg" android:gravity="center" android:text="开饭啦!" android:textColor="@android:color/white" android:visibility="gone"/>
</RelativeLayout>
</RelativeLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ImageView iv_horn;
private TextView tv_left_content,tv_right_content;
private MyBroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
iv_horn=findViewById(R.id.iv_horn);
tv_left_content=findViewById(R.id.tv_left_content);
tv_right_content=findViewById(R.id.tv_right_content);
iv_horn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv_right_content.setText("开饭啦!");
Intent intent = new Intent();
// 定义广播的事件类型
intent.setAction("open_rice");
sendBroadcast(intent); //发送广播
}
});
receiver = new MyBroadcastReceiver(); // 实例化广播接收者
String action = "open_rice";
IntentFilter intentFilter = new IntentFilter(action);
registerReceiver(receiver, intentFilter); // 注册广播接收者
}
class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("open_rice")){
tv_left_content.setVisibility(View.VISIBLE);
Log.i("MyBroadcastReceiver", "自定义的广播接收者, " +
"接收到了发送开饭信号的广播消息");
}
Log.i("MyBroadcastReceiver", intent.getAction());
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}