0

我有一个应用程序,页面上有一个温度转换器,一旦使用,就有一个按钮可以切换到新的活动。但是,当我单击按钮更改为模拟器上的第二个活动时,没有任何反应?

package com.example.assignment2project;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import android.content.Intent;
import android.widget.Button;


public class MainActivity extends Activity {
    private EditText text;

    public void onCreate1(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), SecondScreen.class);
                startActivityForResult(myIntent, 0);
            }

        });
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (EditText) findViewById(R.id.editText1);
    }

    // This method is called at button click because we assigned the name to the
    // "OnClick property" of the button
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
                RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
                RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
                if (text.getText().length() == 0) {
                    Toast.makeText(this, "Please enter a valid number", Toast.LENGTH_LONG).show();
                    return;
                }

                float inputValue = Float.parseFloat(text.getText().toString());
                if (celsiusButton.isChecked()) {
                    text.setText(String.valueOf(convertFahrenheitToCelsius(inputValue)));
                    celsiusButton.setChecked(false);
                    fahrenheitButton.setChecked(true);
                } else {
                    text.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue)));
                     fahrenheitButton.setChecked(false);
                     celsiusButton.setChecked(true);
                }
                break;
        }
    }

    // Converts to celsius
    private float convertFahrenheitToCelsius(float fahrenheit) {
        return ((fahrenheit - 32) * 5 / 9);
    }

    // Converts to fahrenheit
    private float convertCelsiusToFahrenheit(float celsius) {
        return ((celsius * 9) / 5) + 32;
    }
} 
4

3 回答 3

6

你有一个onCreate方法和一个onCreate方法。Android 只是调用该onCreate方法,而不是onCreate1. 您需要重命名onCreate1onCreate删除另一个 onCreate 才能使其正常工作。

编辑:并@Override在新的上使用注释onCreate

于 2013-04-22T13:33:54.573 回答
1

使用此代码

Button next = (Button) findViewById(R.id.BUTTON_ID);
      next.setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
              Intent myIntent = new Intent(getApplicationContext, SecondScreen.class);
              startActivity(myIntent);
          }

      });

把它放在你的 oncreate 上

您需要像这样在清单上添加第二个活动

<activity android:name=".SecondScreen" android:label="@string/app_name">
        </activity>
于 2013-04-22T13:40:52.190 回答
0

adding to previous answers: you can create button in your xml layout as much as you want, this doesn't have relation with the oncreate method.

all you have to do to use these button in your mainactivity java file is to initialize it like this:

 Button name=(Button)findViewById(R.id."button name or id in your xml file");

you can press "Ctrl plus space" in the same time after "R.id." to get you all the objects you have created in the xml file and choose what you want. no to switch to another activity do this in your onclicklistener.

       startActivity(new Intent("yourcurrent activity".this, "the name of activity you want to switch to".class));

which in this case is:

 startActivity(new Intent(MainActivity.this,SecondScreen.class));

then go to you AndroidManifest file and type this inside the application part...you can type it at the very end of the application part.

  <activity android:name=".the name of your second activity"/>

which in your case is:

 <activity android:name=".Secondscreen"/>

you have to do this whenever creating a new activity.

then in your second activity you should tell what xml file you are viewing so you should do this in the second class activity, just like default of the mainacitivty once you have created it.

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout."name of your xml file you want to view");

and i suggest you have a look at this tutorial: http://www.youtube.com/watch?v=q6-4E1JGT_k

于 2013-04-22T14:16:04.813 回答