gpt4 book ai didi

java - onClickListener 无法在 Fragment 中工作

转载 作者:行者123 更新时间:2023-12-01 09:05:07 25 4
gpt4 key购买 nike

当我单击其中一个 fragment 中的按钮时,没有任何反应,监听器无法工作。每当我输入按钮尝试转换到按钮时,它都不会像应该的那样变成紫色。

Java:

public class CollegeMatch extends Fragment {
TextView resuLTS;
EditText actorsat;
EditText rank;
EditText gpa;
EditText apclasses;
public Button calcButton;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_college_match, container, false);

resuLTS = (TextView) view.findViewById(R.id.resuLTS);
actorsat = (EditText) view.findViewById(R.id.actorsat);
gpa = (EditText) view.findViewById(R.id.gpa);
rank = (EditText) view.findViewById(R.id.rank);
apclasses = (EditText) view.findViewById(R.id.apclasses);
Button calcButton;
calcButton = (Button) view.findViewById(R.id.calcButton);
calcButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view2) {
float act = Float.parseFloat(actorsat.getText().toString());
float ap = Float.parseFloat(apclasses.getText().toString());
float rk = Float.parseFloat(rank.getText().toString());
float gp = Float.parseFloat(gpa.getText().toString());
float res = act + ap + rk + gp;
resuLTS.setText(Float.toString(res));
}
});
return view;
}
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.CollegeMatch"
android:background="#bdeaf4"
android:id="@+id/calcBTN">

<!-- TODO: Update blank fragment layout -->

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Find colleges!"
android:id="@+id/textView134"
android:layout_marginTop="50dp"
android:textColor="#000000"
android:textSize="37dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:typeface="serif"
android:textStyle="bold" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/actorsat"
android:layout_marginTop="57dp"
android:autoText="false"
android:hint="Enter Composite Score "
android:layout_below="@+id/textView134"
android:layout_alignLeft="@+id/gpa"
android:layout_alignStart="@+id/gpa" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ACT Composite"
android:id="@+id/textView135"
android:textColor="@color/abc_input_method_navigation_guard"
android:textSize="25dp"
android:layout_alignBottom="@+id/actorsat"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="46dp"
android:layout_marginStart="46dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unweighted GPA"
android:id="@+id/textView136"
android:textColor="@color/abc_input_method_navigation_guard"
android:textSize="25dp"
android:layout_marginTop="35dp"
android:layout_below="@+id/textView135"
android:layout_alignLeft="@+id/textView135"
android:layout_alignStart="@+id/textView135" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class Rank (Top _ %)"
android:id="@+id/textView138"
android:textColor="@color/abc_input_method_navigation_guard"
android:textSize="25dp"
android:layout_marginTop="47dp"
android:layout_below="@+id/textView136"
android:layout_alignLeft="@+id/textView136"
android:layout_alignStart="@+id/textView136" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/rank"
android:layout_above="@+id/tex554"
android:layout_alignLeft="@+id/apclasses"
android:layout_alignStart="@+id/apclasses"
android:hint="Enter Here (omit % sign) " />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total # of AP classes"
android:id="@+id/tex554"
android:textColor="@color/abc_input_method_navigation_guard"
android:textSize="25dp"
android:layout_marginTop="39dp"
android:layout_below="@+id/textView138"
android:layout_alignLeft="@+id/textView138"
android:layout_alignStart="@+id/textView138" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/apclasses"
android:hint="Enter number here"
android:layout_alignBottom="@+id/tex554"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Match Me!"
android:id="@+id/calcButton"
android:layout_marginTop="57dp"
android:background="#ff0000"
android:textColor="#ffffff"
android:textSize="50dp"
android:layout_below="@+id/apclasses"
android:layout_alignLeft="@+id/textView134"
android:layout_alignStart="@+id/textView134" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your College Match Results:"
android:id="@+id/textView155"
android:textColor="@color/abc_input_method_navigation_guard"
android:textSize="32dp"
android:layout_marginTop="46dp"
android:textStyle="normal|bold|italic"
android:layout_below="@+id/calcButton"
android:layout_centerHorizontal="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/gpa"
android:hint="Enter GPA here"
android:layout_above="@+id/textView138"
android:layout_alignLeft="@+id/rank"
android:layout_alignStart="@+id/rank" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/resuLTS"
android:layout_below="@+id/textView155"
android:layout_alignLeft="@+id/textView155"
android:layout_alignStart="@+id/textView155"
android:layout_marginTop="76dp"
android:layout_alignRight="@+id/textView155"
android:layout_alignEnd="@+id/textView155"
android:textColor="#000000"
android:textIsSelectable="false"
android:textSize="25dp" />

</RelativeLayout>

最佳答案

您正在 onViewCreated() 方法中执行操作。创建 View 后会触发此方法,因此它不会应用您的内容。

将代码移至 onCreateView() 内,如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_college_match, container, false);

//do stuffs, for example:

resuLTS = (TextView) view.findViewById(R.id.resuLTS);
actorsat = (EditText) view.findViewById(R.id.actorsat);
gpa = (EditText) view.findViewById(R.id.gpa);
rank = (EditText) view.findViewById(R.id.rank);
apclasses = (EditText) view.findViewById(R.id.apclasses);

Button calcbutton = (Button) view.findViewById(R.id.calcbutton);
calcbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View view2) {
float act = Float.parseFloat(actorsat.getText().toString());
float ap = Float.parseFloat(apclasses.getText().toString());
float rk = Float.parseFloat(rank.getText().toString());
float gp = Float.parseFloat(gpa.getText().toString());
float res = act + ap +rk +gp;
resuLTS.setText(Float.toString(res));

}

});
return view;
}

这应该可行(我已经用完了编译器,所以对于我在这里的任何问题,这可能并不完美)

关于java - onClickListener 无法在 Fragment 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41348095/

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