【问题标题】:switching between activities in java eclipse在 java eclipse 中的活动之间切换
【发布时间】:2013-04-22 13:30:57
【问题描述】:

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

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;
    }
} 

【问题讨论】:

    标签: java android eclipse android-activity


    【解决方案1】:

    您有一个onCreate1 方法和一个onCreate 方法。 Android 只调用onCreate 方法,而不是onCreate1。您需要将 onCreate1 重命名为 onCreate 并删除另一个 onCreate 才能使其工作。

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

    【讨论】:

    • & 使用 @Override 符号来检查。 ;)
    • 好的,谢谢,会试试,这是否意味着您只能在每个活动上使用一个按钮?因为我有两个,但你只能在创建时使用一个?
    • 干杯兄弟它成功了!但是另一个按钮呢,因为它现在会关闭应用程序,而不是计算摄氏度或华氏度的温度
    • 如果您有新问题,请提出新问题
    【解决方案2】:

    使用此代码

    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>
    

    【讨论】:

    • 我已将其添加到清单中,而不是标签 - 请问标签是什么?
    • 只需查看 app_name 的 res/values/string
    【解决方案3】:

    添加到以前的答案: 您可以根据需要在 xml 布局中创建按钮,这与 oncreate 方法无关。

    要在 mainactivity java 文件中使用这些按钮,您只需像这样初始化它:

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

    您可以在“R.id”之后同时按“Ctrl 加空格”。获取您在 xml 文件中创建的所有对象并选择您想要的。 否切换到另一个活动在您的 onclicklistener 中执行此操作。

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

    在这种情况下是:

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

    然后转到您的 AndroidManifest 文件并在应用程序部分中输入它...您可以在应用程序部分的最后输入它。

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

    在你的情况下是:

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

    您必须在创建新活动时执行此操作。

    然后在您的第二个活动中,您应该告诉您正在查看的 xml 文件,因此您应该在第二个活动中执行此操作,就像创建后的 mainacitivty 的默认设置一样。

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

    我建议你看看这个教程: http://www.youtube.com/watch?v=q6-4E1JGT_k

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多