0

我有一个 EditText 、一个 textView 和一个按钮,

当触摸按钮时,我需要将我的 textView 设置为我的 EditText 的值,但是我有一个变量未被识别[抱歉,这里是 android 上的菜鸟]

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

        //buttons
        benefixPaButtonPressed(); 
        benefixAdultButtonPressed();
        xynthaButtonPressed(); 
        calculateButtonPressed();

        //textField == EditText
        final EditText et;
        et = (EditText)findViewById(R.id.editText1);
    }

    public void calculateButtonPressed() {
        // TODO Auto-generated method stub
        Button button = (Button) findViewById(R.id.result);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Log.d("myTag",  "result biatch");

                String text=et.getText().toString();

                Toast msg = Toast.makeText(getBaseContext(),text, Toast.LENGTH_LONG);
                 msg.show();

            }

        });

    }
}

所以与String text=et.getText().toString(); 我有错误一致et cannot be resolved

那么...如何声明我的 et Edit 文本以便按钮类可以看到它?

多谢!

4

3 回答 3

2

相反,让 et 成为一个字段,在那里使用 final 修饰符对我来说毫无意义。

public class XynthaCalcTestActivity extends Activity {

    private EditText et;

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

        //buttons
        benefixPaButtonPressed(); 
        benefixAdultButtonPressed();
        xynthaButtonPressed(); 
        calculateButtonPressed();

        //textField == EditText
        et = (EditText)findViewById(R.id.editText1);
    }

    public void calculateButtonPressed() {
        // TODO Auto-generated method stub
        Button button = (Button) findViewById(R.id.result);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Log.d("myTag",  "result biatch");

                String text=et.getText().toString();

                Toast msg = Toast.makeText(getBaseContext(),text, Toast.LENGTH_LONG);
                 msg.show();

            }

        });

    }
}
于 2012-05-09T07:41:51.823 回答
1

将 Edittext 字段“et”定义为类变量。

于 2012-05-09T07:43:56.500 回答
1

试试这个代码,它可以帮助你......

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

    //buttons
   // benefixPaButtonPressed(); 
   // benefixAdultButtonPressed();
   // xynthaButtonPressed(); 
   // calculateButtonPressed();

    //textField == EditText

    final EditText editText = (EditText)findViewById(R.id.editText1);
    final TextView textView = (TextView)findViewById(R.id.textview);
    Button button = (Button)findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            String text = editText.getText().toString();
            textView.setText(text);
            Toast.makeText(XynthaCalcTestActivity.this,text, Toast.LENGTH_LONG).show();
        }

    });
}
}
于 2012-05-09T07:55:25.390 回答