按钮未打开新屏幕

时间:2016-07-18 22:06:46

标签: android

不知道还有什么要添加的。当我按下应该打开一个新屏幕的按钮时,没有任何反应。

此外,EditText在返回MainActivity屏幕后没有保存任何输入。

代码顺序: 主要活动 NewScreenActivity 的Manifest.xml (不包括每个活动的xml,但如果必要的话可以包括)

两个活动都在同一个包中。

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText userText;
    Button buttonToNewScreen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userText = (EditText) findViewById(R.id.userText);
        buttonToNewScreen = (Button) findViewById(R.id.buttonToNewScreen);
        userText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = userText.getText().toString();
                Intent toNewScreen = new Intent(MainActivity.this, NewScreenActivity.class);
                toNewScreen.putExtra("input", input);
                startActivity(toNewScreen);
            }
        });
    }
}
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

public class NewScreenActivity extends AppCompatActivity {

    String userInput;
    EditText output;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_screen);
        Intent thisIntent = getIntent();
        userInput = thisIntent.getStringExtra("input");
        output = (EditText) findViewById(R.id.output);
        output.setText(userInput);

    }
}

(由Android Studio生成)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.intri.firstexample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NewScreenActivity"></activity>
    </application>

</manifest>

感谢任何可以提供帮助的人

1 个答案:

答案 0 :(得分:1)

buttonToNewScreen = (Button) findViewById(R.id.buttonToNewScreen);
userText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = userText.getText().toString();
                Intent toNewScreen = new Intent(MainActivity.this, NewScreenActivity.class);
                toNewScreen.putExtra("input", input);
                startActivity(toNewScreen);
            }
        });

而不是userTextEditText)将onClickListener设置为buttonToNewScreen