gpt4 book ai didi

java - Android fragment 代码问题

转载 作者:行者123 更新时间:2023-12-01 13:50:00 24 4
gpt4 key购买 nike

我想知道如何让下面的代码与我的项目一起使用,以便将两者结合起来,以便它可以工作。我所拥有的是应用程序中实现的抽屉导航

第一个使用 fragment 的 Java 代码:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PagesFragment extends Fragment {

public PagesFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_pages, container, false);

return rootView;
}
}

使用 Activity 的第二个 Java 代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AppointmentActivity extends Activity {
Button sendEmail;
EditText msg;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appointment_layout);

sendEmail = (Button) findViewById(R.id.sndBtn);
sendEmail.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
msg = (EditText) findViewById(R.id.msgTxt);
String message = msg.getText().toString();
sendEmail(message);
}

});
}

protected void sendEmail(String message) {

String[] to=new String[]{"Shop@email.com"};
String subject=("Shop Appointment");
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, message);
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Gmail"));
}
}

我自己尝试过将两者结合起来,但我没有太多处理 android 的经验,不知道如何使这两个代码并行工作,而不会给我带来压力。任何事情都会有帮助!

最佳答案

要组合这些代码,您需要像 FragmentActivity 一样声明您的 Activity(它将“托管”您的 Fragment)。请参阅这个答案:https://stackoverflow.com/a/10609839/2668136
Google training Fragment
另外Google documentation FragmentActivity

希望这有帮助。

关于java - Android fragment 代码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20038198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com