save code
This commit is contained in:
@@ -44,6 +44,10 @@
|
||||
android:value="@string/native_lib_name" />
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name=".SecondActivity"
|
||||
android:exported="false" />
|
||||
|
||||
<provider
|
||||
android:authorities="${applicationId}.provider"
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package com.khronos.vulkan_samples;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Debug;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.activity.EdgeToEdge;
|
||||
@@ -17,8 +21,8 @@ public class MainActivity extends GameActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
String native_lib_name = getResources().getString(R.string.native_lib_name);
|
||||
try {
|
||||
String native_lib_name = getResources().getString(R.string.native_lib_name);
|
||||
System.loadLibrary(native_lib_name);
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
Toast.makeText(getApplicationContext(), "Native code library failed to load.", Toast.LENGTH_SHORT).show();
|
||||
@@ -29,6 +33,47 @@ public class MainActivity extends GameActivity {
|
||||
sendArgumentsToPlatform(argArray);
|
||||
super.onCreate(savedInstanceState);
|
||||
Log.i("MainActivity", "onCreate: ");
|
||||
|
||||
// 添加按钮到布局
|
||||
addButtonToLayout();
|
||||
}
|
||||
|
||||
private void addButtonToLayout() {
|
||||
// 创建按钮
|
||||
Button button = new Button(this);
|
||||
button.setText("Vulkan 按钮");
|
||||
|
||||
// 设置按钮位置(使用绝对坐标)
|
||||
button.setX(50); // 距离左边50像素
|
||||
button.setY(50); // 距离顶部50像素
|
||||
|
||||
// 设置按钮点击事件
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
onButtonClicked();
|
||||
}
|
||||
});
|
||||
|
||||
// 将按钮添加到 GameActivity 的布局中(不会覆盖 Vulkan 表面)
|
||||
addContentView(button, new ViewGroup.LayoutParams(
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
private void onButtonClicked() {
|
||||
Toast.makeText(this, "Vulkan 按钮被点击了!", Toast.LENGTH_SHORT).show();
|
||||
nativeButtonClicked();
|
||||
// 启动SecondActivity
|
||||
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
|
||||
// 可以传递数据
|
||||
intent.putExtra("message", "Hello from MainActivity!");
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
private native void nativeButtonClicked();
|
||||
|
||||
private native void sendArgumentsToPlatform(String[] args);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.khronos.vulkan_samples;
|
||||
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);
|
||||
|
||||
TextView textView = findViewById(R.id.textViewSecond);
|
||||
Button buttonBack = findViewById(R.id.buttonBack);
|
||||
|
||||
// 接收从MainActivity传递的数据
|
||||
Intent intent = getIntent();
|
||||
if (intent != null && intent.hasExtra("message")) {
|
||||
String message = intent.getStringExtra("message");
|
||||
textView.setText(message);
|
||||
}
|
||||
|
||||
buttonBack.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// 返回数据到MainActivity
|
||||
Intent returnIntent = new Intent();
|
||||
returnIntent.putExtra("returnData", "Hello back from SecondActivity!");
|
||||
setResult(RESULT_OK, returnIntent);
|
||||
|
||||
// 结束当前Activity
|
||||
finish();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?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"
|
||||
android:padding="16dp"
|
||||
android:gravity="center">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textViewSecond"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Second Activity"
|
||||
android:textSize="24sp"
|
||||
android:layout_marginBottom="32dp"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/buttonBack"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Back to Main Activity"/>
|
||||
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user