关注公众号,回复“安卓活动”
版本信息和创建项目
D:\installSoftware\nox\Nox\bin,这里使用夜深模拟器
打开cmd,
//以下为命令行
d:
cd D:\installSoftware\nox\Nox\bin
nox_adb.exe connect 127.0.0.1:62001
这样就可以了
整体代码
<?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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toast" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="exit" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转页面second" />
</LinearLayout>
<?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=".SecondActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="32dp"
android:text="传递参数" />
<Button
android:id="@+id/btn_second_exit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second_exit" />
</LinearLayout>
package com.example.acitivitytest;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
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 button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "you click it", Toast.LENGTH_LONG).show();
}
});
//退出
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
// 跳转界面
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 显式
// Intent intent = new Intent(MainActivity.this,SecondActivity.class);
// 隐式
// Intent intent = new Intent(".MY_ACTION");
// 跳转
// Intent intent = new Intent(Intent.ACTION_VIEW);
// intent.setData(Uri.parse("http://www.baidu.com"));
// 向下一个界面传值
// Intent intent = new Intent(MainActivity.this,SecondActivity.class);
// String data = "this is value";
// intent.putExtra("key",data);
// startActivity(intent);
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("key","this is value");
startActivityForResult(intent,1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1:
String return_data = data.getStringExtra("data_return");
Toast.makeText(MainActivity.this,return_data,Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
//菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.add_item:
Toast.makeText(MainActivity.this,"click add",Toast.LENGTH_LONG).show();
break;
case R.id.remove_item:
Toast.makeText(MainActivity.this,"click remove",Toast.LENGTH_LONG).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
package com.example.acitivitytest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button button2 = (Button) findViewById(R.id.btn_second_exit);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("data_return","I am back !!!!");
setResult(RESULT_OK,intent);
finish();
}
});
// 从上一层传来的值
Intent intent = getIntent();
// 取出值
String value = intent.getStringExtra("key");
TextView textView = findViewById(R.id.textView);
textView.setText(value);
}
// 返回界面产值
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent();
intent.putExtra("data_return","I am back !!!!");
setResult(RESULT_OK,intent);
finish();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.acitivitytest">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@android:drawable/btn_star_big_on"
android:label="@string/app_name"
android:roundIcon="@android:drawable/ic_notification_overlay"
android:supportsRtl="true"
android:theme="@style/Theme.AcitivityTest"
tools:targetApi="31">
<!-- exported导出,true被其他应用所用-->
<activity
android:name=".SecondActivity"
android:exported="true">
<intent-filter>
<action android:name=".MY_ACTION"></action>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
弹框
// 弹框
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "you click it", Toast.LENGTH_LONG).show();
}
});
退出
//退出
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
菜单
//菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return super.onCreateOptionsMenu(menu);
}
跳转界面
在以上代码里面