0

我不知道如何解决整数问题,如何发送在 EditText 中设置的值并在 Arduino 上的 Android 按下按钮后发送。

'

public class MainActivity extends Activity {
private static final String DEFAULT_DEVICE_ADDRESS = "00:12:10:16:12:14";
private Context context;
    TextView value, display;
    EditText nasttemp;
    Button button;  

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    final int nastaviTemp = Integer.parseInt(nasttemp.getText().toString());

        context = this;
        display = (TextView) findViewById(R.id.nasttemp);

        //////////// Button SET /////////////////// 
          Button button = (Button) findViewById(R.id.button);
            button.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 Amarino.sendDataToArduino(context, DEFAULT_DEVICE_ADDRESS, 'o',
                         new int[] { nastaviTemp );                    
             }      
            }); 
    ////////////////////////////////////////////                

'

4

2 回答 2

0

I found a solution for my code and here is it for others who will face with it.

    TextView vrednostTemp, display;
EditText nasttemp;
Button Nastavi;

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

    vrednostTemp = (TextView) findViewById(R.id.value);
    nasttemp = (EditText) findViewById(R.id.nasttemp);
    display = (TextView) findViewById(R.id.stanjeTemp);
    Nastavi = (Button) findViewById(R.id.button);
    context = this; 

    Nastavi.setOnClickListener(new View.OnClickListener() {         
        @Override
        public void onClick(View v) {
            int nastaviTemp = Integer.valueOf(nasttemp.getText().toString());
            display.setText("" + nastaviTemp);
            Amarino.sendDataToArduino(context,DEVICE_ADDRESS, 'o',
                   new int[] { nastaviTemp });              
            }
        });
    }
于 2013-04-17T06:46:07.293 回答
0

首先是你做错了:-

final int nastaviTemp = Integer.parseInt(nasttemp.getText().toString());
display = (TextView) findViewById(R.id.nasttemp);

正确的是

display = (TextView) findViewById(R.id.nasttemp);
final int nastaviTemp = Integer.parseInt(nasttemp.getText().toString());

在初始化之前如何获得价值。

于 2013-03-31T09:19:30.773 回答