parent
3bcf03e13a
commit
593305bdf4
@ -0,0 +1,74 @@
|
|||||||
|
package com.example.doitnow;
|
||||||
|
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.view.Gravity;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.EditText;
|
||||||
|
import android.widget.LinearLayout;
|
||||||
|
import android.widget.ScrollView;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.fragment.app.Fragment;
|
||||||
|
|
||||||
|
public class AIFragment extends Fragment {
|
||||||
|
|
||||||
|
private EditText userInput;
|
||||||
|
private Button sendButton;
|
||||||
|
private LinearLayout chatContainer;
|
||||||
|
private ScrollView scrollView;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||||
|
@Nullable ViewGroup container,
|
||||||
|
@Nullable Bundle savedInstanceState) {
|
||||||
|
View view = inflater.inflate(R.layout.fragment_ai, container, false);
|
||||||
|
|
||||||
|
userInput = view.findViewById(R.id.user_input);
|
||||||
|
sendButton = view.findViewById(R.id.send_button);
|
||||||
|
chatContainer = view.findViewById(R.id.chat_container);
|
||||||
|
scrollView = view.findViewById(R.id.scrollView);
|
||||||
|
|
||||||
|
sendButton.setOnClickListener(v -> {
|
||||||
|
String inputText = userInput.getText().toString().trim();
|
||||||
|
if (!inputText.isEmpty()) {
|
||||||
|
addMessage(inputText, 0xFFE6D6F3,true); // 使用者
|
||||||
|
addMessage("暫未接入 AI", 0xFFF0F0F0,false); // AI
|
||||||
|
userInput.setText("");
|
||||||
|
scrollToBottom();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addMessage(String text, int backgroundColor,boolean alignRight) {
|
||||||
|
TextView messageView = new TextView(getContext());
|
||||||
|
messageView.setText(text);
|
||||||
|
messageView.setTextSize(16);
|
||||||
|
messageView.setPadding(20, 12, 20, 12);
|
||||||
|
messageView.setBackgroundColor(backgroundColor);
|
||||||
|
|
||||||
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
|
||||||
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||||
|
ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||||
|
params.setMargins(0, 8, 0, 8);
|
||||||
|
if (alignRight){
|
||||||
|
params.gravity= Gravity.END;
|
||||||
|
}else{
|
||||||
|
params.gravity=Gravity.START;
|
||||||
|
}
|
||||||
|
messageView.setLayoutParams(params);
|
||||||
|
|
||||||
|
chatContainer.addView(messageView);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scrollToBottom() {
|
||||||
|
scrollView.post(() -> scrollView.fullScroll(View.FOCUS_DOWN));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue