Page4: 人工智能

This commit is contained in:
icewithcola 2024-05-23 20:15:49 +08:00
parent 6df25bed82
commit 58d9a791bc
11 changed files with 239 additions and 9 deletions

2
.gitignore vendored
View file

@ -13,3 +13,5 @@
.externalNativeBuild .externalNativeBuild
.cxx .cxx
local.properties local.properties
app/release
/.idea/inspectionProfiles/Project_Default.xml

View file

@ -14,6 +14,12 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.Android101" android:theme="@style/Theme.Android101"
tools:targetApi="31"> tools:targetApi="31">
<activity
android:name=".Page4OtherActivity"
android:exported="false" />
<activity
android:name=".Page5"
android:exported="false" />
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:exported="true"> android:exported="true">
@ -32,7 +38,6 @@
<activity <activity
android:name=".MainActivity2" android:name=".MainActivity2"
android:exported="false" /> android:exported="false" />
<activity <activity
android:name=".todoList.AddPage" android:name=".todoList.AddPage"
android:exported="false" android:exported="false"
@ -45,15 +50,14 @@
android:theme="@style/Theme.TODOList"> android:theme="@style/Theme.TODOList">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
<meta-data android:name="android.app.shortcuts"
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" /> android:resource="@xml/shortcuts" />
</activity> </activity>
</application> </application>
</manifest> </manifest>

View file

@ -64,7 +64,7 @@ public class MainActivity extends AppCompatActivity {
textViewAppendString(tv,"*************************\nfinished"); textViewAppendString(tv,"*************************\nfinished");
// Initialize Page Helper // Initialize Page Helper
pageHelper = new PageHelper(this,null,MainActivity2.class); pageHelper = new PageHelper(this,null,MainActivity2.class,this);
Button button = findViewById(R.id.Page1NextPage); Button button = findViewById(R.id.Page1NextPage);
button.setEnabled(true); button.setEnabled(true);

View file

@ -1,8 +1,17 @@
package uk.kagurach.android101; package uk.kagurach.android101;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.activity.EdgeToEdge; import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets; import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
@ -11,6 +20,8 @@ import androidx.core.view.WindowInsetsCompat;
public class Page4 extends AppCompatActivity { public class Page4 extends AppCompatActivity {
PageHelper pageHelper; PageHelper pageHelper;
ActivityResultLauncher mLauncher;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -21,6 +32,47 @@ public class Page4 extends AppCompatActivity {
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets; return insets;
}); });
pageHelper = new PageHelper(this, Page3.class,Page5.class);
Button askAI = findViewById(R.id.P4AskAI);
askAI.setOnClickListener(new ButtonHandler());
Button nextPage = findViewById(R.id.P4PageButton);
nextPage.setOnClickListener(pageHelper.pageButtonHandler);
nextPage.setOnLongClickListener(pageHelper.longClickHandler);
mLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result!=null && result.getResultCode() == RESULT_OK ){
assert result.getData() != null;
Bundle bundle = result.getData().getExtras();
if (bundle!=null) {
String response = bundle.getString("reply");
TextView tv = findViewById(R.id.P4Result);
tv.setText(response);
}
}
}
);
}
private void callOtherActivity(){
EditText editText = findViewById(R.id.P4Ask);
String request = editText.getText().toString();
Intent intent = new Intent(this,Page4OtherActivity.class);
Bundle bundle = new Bundle();
bundle.putString("request_content",request);
intent.putExtras(bundle);
mLauncher.launch(intent);
}
private final class ButtonHandler implements View.OnClickListener
{
@Override
public void onClick(View v){
callOtherActivity();
}
} }

View file

@ -0,0 +1,44 @@
package uk.kagurach.android101;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class Page4OtherActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
if (bundle == null) throw new NullPointerException();
String content = bundle.getString("request_content");
if (content!=null){
content = content.replace("","?")
.replace("?","!")
.replace("","")
.replace("","");
if (!content.contains("!")){
content = "对不起我不知道";
}
}else {
content = "对不起坏掉了";
}
Intent intent = new Intent();
Bundle bundle1 = new Bundle();
bundle1.putString("reply",content);
intent.putExtras(bundle1);
setResult(RESULT_OK,intent);
finish();
}
private void reply(String s){
}
}

View file

@ -0,0 +1,24 @@
package uk.kagurach.android101;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class Page5 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_page5);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}

View file

@ -2,6 +2,7 @@ package uk.kagurach.android101;
import static androidx.core.content.ContextCompat.startActivity; import static androidx.core.content.ContextCompat.startActivity;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.view.View; import android.view.View;
@ -11,6 +12,8 @@ public class PageHelper {
private final Class<?> _prev; private final Class<?> _prev;
private final Class<?> _next; private final Class<?> _next;
private Activity _activity = null;
public final LongClickHandler longClickHandler = new LongClickHandler(); public final LongClickHandler longClickHandler = new LongClickHandler();
public final PageButtonHandler pageButtonHandler = new PageButtonHandler(); public final PageButtonHandler pageButtonHandler = new PageButtonHandler();
@ -20,6 +23,14 @@ public class PageHelper {
_next = next; _next = next;
} }
PageHelper(Context curr, Class<?> prev, Class<?> next,Activity activity){
_curr = curr;
_prev = prev;
_next = next;
_activity = activity;
}
void goPrev(){ void goPrev(){
if (_prev==null){ if (_prev==null){
return; return;
@ -36,6 +47,19 @@ public class PageHelper {
startActivity(_curr,myIntent,null); startActivity(_curr,myIntent,null);
} }
void goNextFinish(){
if (_activity == null){
throw new IllegalStateException("activity is null, cannot finish the activity");
}
if (_next==null){
return;
}
Intent myIntent = new Intent(_curr, _next);
startActivity(_curr,myIntent,null);
_activity.finish();
}
private final class LongClickHandler implements View.OnLongClickListener private final class LongClickHandler implements View.OnLongClickListener
{ {
@Override @Override
@ -48,7 +72,11 @@ public class PageHelper {
{ {
@Override @Override
public void onClick(View v){ public void onClick(View v){
if (_activity == null) {
goNext(); goNext();
}else {
goNextFinish();
}
} }
} }
} }

View file

@ -7,4 +7,64 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".Page4"> tools:context=".Page4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<View
android:layout_width="match_parent"
android:layout_height="50dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="@string/user"/>
<EditText
android:layout_width="match_parent"
android:layout_height="200dp"
android:textSize="40sp"
android:id="@+id/P4Ask"
android:importantForAutofill="no"
android:text="你吃了吗?"
android:textColor="@color/tfp"
android:inputType="text" />
<View
android:layout_width="match_parent"
android:layout_height="50dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="@string/ai"/>
<TextView
android:layout_width="match_parent"
android:textSize="40sp"
android:layout_height="200dp"
android:id="@+id/P4Result"
android:textColor="@color/tfb"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="140dp"
android:layout_height="match_parent"
android:id="@+id/P4AskAI"
android:text="@string/ask_ai"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/P4PageButton"
android:text="@string/next_page"
/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Page5">
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -11,4 +11,7 @@
<string name="second_delete">确认删除</string> <string name="second_delete">确认删除</string>
<string name="todo_app">任务清单</string> <string name="todo_app">任务清单</string>
<string name="todo_app_short_label">启动任务清单</string> <string name="todo_app_short_label">启动任务清单</string>
<string name="user">用户</string>
<string name="ai">AI</string>
<string name="ask_ai">询问AI</string>
</resources> </resources>

View file

@ -21,4 +21,7 @@
<string name="kaculate" translatable="false">弱智计算器</string> <string name="kaculate" translatable="false">弱智计算器</string>
<string name="Backspace" translatable="false">退格</string> <string name="Backspace" translatable="false">退格</string>
<string name="todo_app_short_label">Open TODO</string> <string name="todo_app_short_label">Open TODO</string>
<string name="user">User</string>
<string name="ai">AI</string>
<string name="ask_ai">ASK AI</string>
</resources> </resources>