簡體   English   中英

建表Android Studio有問題

[英]Have a problem on creating table Android Studio

我正在嘗試制作一個簡單的應用程序。 該程序應該創建一個具有給定行號和列號的表。 我在下面添加我的 Java 和 xml 文件:

public class MainActivity extends AppCompatActivity {

    private Button btnCreate, btnCalculate, btnReset, btnExit;
    private EditText txtColumn, txtRow;
    private LinearLayout linLayout, mainLayout;
    private TextView txtResult;
    private Random random;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnCreate = (Button) findViewById(R.id.btnCreate);
        btnCalculate = (Button) findViewById(R.id.btnCalculate);
        btnReset = (Button) findViewById(R.id.btnReset);
        btnExit = (Button) findViewById(R.id.btnExit);

        txtColumn = (EditText) findViewById(R.id.txtColumn);
        txtRow = (EditText) findViewById(R.id.txtRow);
        txtResult = (TextView) findViewById(R.id.txtResult);
        txtColumn.requestFocus();

        random = new Random();

        LinearLayout mainLayout = findViewById(R.id.mainLayout);
        TableLayout table = new TableLayout(this);

        btnCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                table.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                table.setShrinkAllColumns(true);
                table.setStretchAllColumns(true);
                int columnNumber = Integer.parseInt(txtColumn.getText().toString());
                int rowNumber = Integer.parseInt(txtRow.getText().toString());

                for (int i=0; i < rowNumber; i++) {
                    TableRow row = new TableRow(MainActivity.this);
                    for (int j=0; j < columnNumber; j++) {
                        int value = random.nextInt(100) + 1;
                        TextView tv = new TextView(MainActivity.this);
                        tv.setText(String.valueOf(value));
                        row.addView(tv);
                    }
                    table.addView(row);
                }

                mainLayout.addView(table);

            }
        });


        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.exit(0);
            }
        });
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                txtColumn.setText(null);
                txtRow.setText(null);
                txtResult.setText(null);
            }
        });
    }
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/txtRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter row number"
        android:inputType="textCapWords"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/txtColumn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter column number"
        android:inputType="textCapWords"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Result"
        android:background="#E91E63"
        android:textSize="18sp"
        />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnCreate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CREATE"/>

        <Button
            android:id="@+id/btnCalculate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CALCULATE" />

        <Button
            android:id="@+id/btnReset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RESET" />
        <Button
            android:id="@+id/btnExit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="EXIT" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/mainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


    </LinearLayout>

</LinearLayout>

這是我收到的錯誤消息:

The specified child already has a parent. You must call removeView() on the child's parent first.

因此,每當我嘗試添加mainLayout.removeView(table); 當我單擊按鈕創建時,代碼沒有任何反應。 謝謝你們。 我將不勝感激任何評論。

您有以下錯誤:

1: mainLayout布局上方的布局讓高度為match_parent 使mainLayout無法顯示。

2:布局mainLayout也將高度設置為match_parent這也是錯誤的。 我想知道你明白match_parent是什么意思嗎?

3:第一次添加成功,但是因為設置了高度為match_parent ,所以看不到,實際上是成功添加了表格 第二次點擊因為已經添加到表格中,會崩潰。

下面的代碼我已經為你修復了,讓我們試試吧。

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/txtRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter row number"
        android:inputType="textCapWords"
        android:textSize="18sp"
        android:text="3"/>

    <EditText
        android:id="@+id/txtColumn"
        android:text="2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter column number"
        android:inputType="textCapWords"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Result"
        android:background="#E91E63"
        android:textSize="18sp"
        />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnCreate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CREATE"/>

        <Button
            android:id="@+id/btnCalculate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CALCULATE" />

        <Button
            android:id="@+id/btnReset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RESET" />
        <Button
            android:id="@+id/btnExit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="EXIT" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/mainLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


    </LinearLayout>

</LinearLayout>

JAVA:

public class MainAct extends AppCompatActivity {

    private Button btnCreate, btnCalculate, btnReset, btnExit;
    private EditText txtColumn, txtRow;
    private LinearLayout linLayout, mainLayout;
    private TextView txtResult;
    private Random random;
    TableLayout table;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnCreate = (Button) findViewById(R.id.btnCreate);
        btnCalculate = (Button) findViewById(R.id.btnCalculate);
        btnReset = (Button) findViewById(R.id.btnReset);
        btnExit = (Button) findViewById(R.id.btnExit);

        txtColumn = (EditText) findViewById(R.id.txtColumn);
        txtRow = (EditText) findViewById(R.id.txtRow);
        txtResult = (TextView) findViewById(R.id.txtResult);
        txtColumn.requestFocus();

        random = new Random();

        LinearLayout mainLayout = findViewById(R.id.mainLayout);


        btnCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (table != null) {
                    mainLayout.removeView(table);
                }
                table = new TableLayout(getBaseContext());

                table.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                table.setShrinkAllColumns(true);
                table.setStretchAllColumns(true);
                int columnNumber = Integer.parseInt(txtColumn.getText().toString());
                int rowNumber = Integer.parseInt(txtRow.getText().toString());

                for (int i=0; i < rowNumber; i++) {
                    TableRow row = new TableRow(MainAct.this);
                    for (int j=0; j < columnNumber; j++) {
                        int value = random.nextInt(100) + 1;
                        TextView tv = new TextView(MainAct.this);
                        tv.setText(String.valueOf(value));
                        row.addView(tv);
                    }
                    table.addView(row);
                }
                if (table != null) {
                    mainLayout.addView(table);
                }
            }
        });


        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.exit(0);
            }
        });
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                txtColumn.setText(null);
                txtRow.setText(null);
                txtResult.setText(null);
            }
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM