Могу ли я вызвать значение другого класса в onCreate ()? - PullRequest
0 голосов
/ 16 февраля 2012

Я пытаюсь выяснить, есть ли способ вызвать "submitValue", созданный в createNewSubmitButton (); в onCreate (); функционировать?

Итак, я хочу отправить значение в EditText в TextView, когда вы нажмете кнопку «Отправить», поэтому я установил там onClick, но я не смог вызвать submitValue в onCreate?

Посмотрите мой пример кода, чтобы понять, что я имею в виду:

package com.lars.myApp;

import com.lars.myApp.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;

public class myAppActivity extends Activity {

    int currentValue =  0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText valueOne = (EditText) findViewById(R.id.valueOne);
        Button addOne = (Button) findViewById(R.id.addOne);
        Button minusOne = (Button) findViewById(R.id.minusOne);

        Button addButton = (Button) findViewById(R.id.add);
        final TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);

        TextView textView = new TextView(this);
        textView.setText("New text");

        addOne.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                addValue();
                valueOne.setText("" + currentValue);
            }
        });

        minusOne.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                minusValue();
                valueOne.setText("" + currentValue);
            }
        });

        addButton.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                tableLayout1.addView(createNewRow());
            }
        });


        // I OUTCOMMENTED THIS, BECAUSE IT DIDN'T WORK, submitValue: "CANNOT BE RESOLVED"

        /*submitValue.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                tableLayout1.addView(createNewTextView(newValue.getText().toString()));
            }

        });*/

    }

    public TableRow createNewRow() {
        final TableRow newRow = new TableRow(this);
        newRow.addView(createNewContainer());
        return newRow;
    }

    public LinearLayout createNewContainer(){
        final LinearLayout newContainer = new LinearLayout(this);
        newContainer.addView(createNewEditableText());
        newContainer.addView(createNewSubmitButton());
        return newContainer;
    }    
    public EditText createNewEditableText() {
        final EditText newValue = new EditText(this);
        newValue.setHint("New");
        return newValue;
    }

    public Button createNewSubmitButton(){
        final Button submitValue = new Button(this);
        submitValue.setText("Submit");
        return submitValue;
    }

    public TextView createNewTextView(String text) {
        final TextView textView = new TextView(this);
        textView.setText(text);
        return textView;
    }

    public void addValue(){
        if(currentValue <= 999){
        currentValue = currentValue + 1;
        }
    }  

    public void minusValue(){
        if(currentValue >= 1){
            currentValue = currentValue - 1;
        }
    }  
}

Я надеюсь, что кто-то может мне помочь

1 Ответ

1 голос
/ 17 февраля 2012

На самом деле, вы не можете вызвать то, что еще не создано в вашем представлении.

Тем не менее, у вас есть 2 варианта:

A - Создайте это в вашем onCreate(...), нодобавьте его позже к вашему представлению

B- Создайте его, добавьте его, но используйте следующий параметр:

sibmitButton.setVisibility(View.GONE);

Отличная вещь в том, что вы можете добавить кнопку, играть с ней(программно), но пользователь не может коснуться этого!Это даже не занимает «пробел» с вашей точки зрения!

А при необходимости просто используйте:

sibmutButton.setVisibility(View.VISIBLE);

Это то, что вы хотите?

...