即使在使用 addView 之后,getChildCount() 总是返回 1
getChildCount() always returning 1 even after using addView
我正在设计一个具有历史功能的简单计算器。之前的所有结果都以文本视图的形式存储,并使用 addView(childview) 添加到线性布局中。问题是,即使在将文本视图添加到线性布局之后,linearlayout.getChildCount() 始终返回 1。为简单起见,我将只在此处添加相关代码。
Calculator.java:
public class Calculator extends Fragment {
TextView resultTextView;
LinearLayout linearLayout;
ScrollView scrollView;
TextView et_temp, sign;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate (R.layout.fragment_calculator, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated (view, savedInstanceState);
initControl(view);
initControlListener();
vibrator=(Vibrator) getActivity ().getSystemService (Context.VIBRATOR_SERVICE);
}
private void initControlListener() {
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("9");
}
});
buttonClear.setOnLongClickListener (new View.OnLongClickListener () {
@Override
public boolean onLongClick(View view) {
onClearHold ();
return false;
}
});
buttonClear.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
onClearButtonClicked ();
}
});
buttonSubstract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
onOperatorButtonClicked("-");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOperatorButtonClicked("+");
}
});
buttonMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOperatorButtonClicked("X");
}
});
buttonDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOperatorButtonClicked("/");
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onEqualButtonClicked();
}
});
buttonDeci.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
onNumberButtonClicked (".");
}
});
buttonReciprocal.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
onSpecialFunctionClick("Reci");
}
});
buttonSqr.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
onSpecialFunctionClick("Sqr");
}
});
buttonSqrRoot.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
onSpecialFunctionClick("SqrRoot");
}
});
linearLayout.setOnLongClickListener (new View.OnLongClickListener () {
@Override
public boolean onLongClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity ());
builder.setMessage("Clear History?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
scrollView.removeAllViews();
scrollView.setFillViewport (true);
scrollView.setClickable (true);
}
});
builder.setNegativeButton ("Cancel", new DialogInterface.OnClickListener () {@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog alert = builder.create();
alert.show();
return false;
}
});
}
//For adding history/views to linear layout
public void createHistory(String str){
if(!str.equals ("")) {
et_temp = new TextView (getActivity ());
et_temp.setGravity (Gravity.RIGHT);
et_temp.setPadding (0, 5, 10, 5);
et_temp.setTextColor (Color.rgb (50, 50, 50));
et_temp.setTextSize (20);
et_temp.setHovered (true);
String x= sign.getText ().toString () + " " + str;
et_temp.setText (x);
Log.e("dsfsd", (String.valueOf (scrollView.getChildCount ())));
linearLayout.addView (et_temp);
}
}
相关xml 文件fragment_calculator:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="?android:windowBackground"
android:orientation="vertical"
android:weightSum="1.0">
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_weight="1"
android:clickable="true"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false">
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:clickable="true"
android:gravity="bottom"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
您呼叫了 scrollView.getChildCount()
而不是 linearLayout.getChildCount()
。
我正在设计一个具有历史功能的简单计算器。之前的所有结果都以文本视图的形式存储,并使用 addView(childview) 添加到线性布局中。问题是,即使在将文本视图添加到线性布局之后,linearlayout.getChildCount() 始终返回 1。为简单起见,我将只在此处添加相关代码。
Calculator.java:
public class Calculator extends Fragment {
TextView resultTextView;
LinearLayout linearLayout;
ScrollView scrollView;
TextView et_temp, sign;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate (R.layout.fragment_calculator, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated (view, savedInstanceState);
initControl(view);
initControlListener();
vibrator=(Vibrator) getActivity ().getSystemService (Context.VIBRATOR_SERVICE);
}
private void initControlListener() {
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("0");
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("1");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("2");
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("3");
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("4");
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("5");
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("6");
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("7");
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("8");
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onNumberButtonClicked("9");
}
});
buttonClear.setOnLongClickListener (new View.OnLongClickListener () {
@Override
public boolean onLongClick(View view) {
onClearHold ();
return false;
}
});
buttonClear.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
onClearButtonClicked ();
}
});
buttonSubstract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
onOperatorButtonClicked("-");
}
});
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOperatorButtonClicked("+");
}
});
buttonMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOperatorButtonClicked("X");
}
});
buttonDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOperatorButtonClicked("/");
}
});
buttonEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onEqualButtonClicked();
}
});
buttonDeci.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
onNumberButtonClicked (".");
}
});
buttonReciprocal.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
onSpecialFunctionClick("Reci");
}
});
buttonSqr.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
onSpecialFunctionClick("Sqr");
}
});
buttonSqrRoot.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
onSpecialFunctionClick("SqrRoot");
}
});
linearLayout.setOnLongClickListener (new View.OnLongClickListener () {
@Override
public boolean onLongClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity ());
builder.setMessage("Clear History?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
scrollView.removeAllViews();
scrollView.setFillViewport (true);
scrollView.setClickable (true);
}
});
builder.setNegativeButton ("Cancel", new DialogInterface.OnClickListener () {@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog alert = builder.create();
alert.show();
return false;
}
});
}
//For adding history/views to linear layout
public void createHistory(String str){
if(!str.equals ("")) {
et_temp = new TextView (getActivity ());
et_temp.setGravity (Gravity.RIGHT);
et_temp.setPadding (0, 5, 10, 5);
et_temp.setTextColor (Color.rgb (50, 50, 50));
et_temp.setTextSize (20);
et_temp.setHovered (true);
String x= sign.getText ().toString () + " " + str;
et_temp.setText (x);
Log.e("dsfsd", (String.valueOf (scrollView.getChildCount ())));
linearLayout.addView (et_temp);
}
}
相关xml 文件fragment_calculator:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="?android:windowBackground"
android:orientation="vertical"
android:weightSum="1.0">
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_weight="1"
android:clickable="true"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false">
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:clickable="true"
android:gravity="bottom"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
您呼叫了 scrollView.getChildCount()
而不是 linearLayout.getChildCount()
。