在android中链接两个activity

发布于 2024-12-07 11:10:27 字数 2229 浏览 0 评论 0原文

我正在编写一个简单的Android应用程序,您在一个框中写下您的名字,然后单击“确定”,一个新页面将显示您的名字...问题是,当您单击“确定”时什么也没有发生。

这里是主要活动

public class Click extends Activity implements OnClickListener{

        @Override
        public void onClick(View v) {
                // TODO Auto-generated method stub
                String TypedText = (String)MyText.getText().toString();
                Intent MyInt = new Intent(this, HelloWorld.class);
                MyInt.putExtra("user", TypedText);
                this.startActivity(MyInt);     
                Bundle Retrive = this.getIntent().getExtras();
                Retrive.getString("user");
                setContentView(R.id.Text);
                TextView TextV = (TextView)findViewById(R.id.Text);
                TextV.setText("user");                   

        }
        android.widget.EditText MyText;

        public void OnCreate (Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
                this.setContentView(R.layout.name_getter);
                MyText = (EditText)this.findViewById(R.id.editText1);
                this.findViewById(R.id.button1);
                android.widget.Button RefBut = (Button)this.findViewById(R.id.button1);
                RefBut.setOnClickListener(this);

                }

,这里是清单

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="edu.calpoly.android.lab1Sada"
     android:versionCode="1"
     android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Click"
                 android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="HelloWorld" ></activity>

    </application>
</manifest>

启动 android 模拟器将启动第一个活动单击,但应用程序不会显示新视图...

I'm coding a simple android app where you write in a box your name then click ok and a new page will show your name... The problem is that when you click ok nothing happens.

Here the main activity

public class Click extends Activity implements OnClickListener{

        @Override
        public void onClick(View v) {
                // TODO Auto-generated method stub
                String TypedText = (String)MyText.getText().toString();
                Intent MyInt = new Intent(this, HelloWorld.class);
                MyInt.putExtra("user", TypedText);
                this.startActivity(MyInt);     
                Bundle Retrive = this.getIntent().getExtras();
                Retrive.getString("user");
                setContentView(R.id.Text);
                TextView TextV = (TextView)findViewById(R.id.Text);
                TextV.setText("user");                   

        }
        android.widget.EditText MyText;

        public void OnCreate (Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
                this.setContentView(R.layout.name_getter);
                MyText = (EditText)this.findViewById(R.id.editText1);
                this.findViewById(R.id.button1);
                android.widget.Button RefBut = (Button)this.findViewById(R.id.button1);
                RefBut.setOnClickListener(this);

                }

And here the manifest

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="edu.calpoly.android.lab1Sada"
     android:versionCode="1"
     android:versionName="1.0">
    <uses-sdk android:minSdkVersion="4" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Click"
                 android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="HelloWorld" ></activity>

    </application>
</manifest>

Starting the android emulator will launch the first activity click but then the app doesn't show the new view...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

权谋诡计 2024-12-14 11:10:27

我不知道你在做什么,但你可以像这样从 Activity 发送/检索数据到另一个 Activity:

为此,你需要理解 意图

来自第一个活动:

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("name", "paresh");
i.putExtra("technology", "android");
startActivity(i);

来自第二个活动:

Bundle extras = getIntent().getExtras();
if (extras == null) {
    return;
}
String strName = extras.getString("name");
String strTechnology = extras.getString("technology");

仍然供您参考,这里是了解更多相同内容的文章: Android 意图

I dont know what you are doing but you can send/retrieve data from/to activity to another activity like this way:

For that you need to understand the concept of Intent.

From First activity:

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("name", "paresh");
i.putExtra("technology", "android");
startActivity(i);

From Second activity:

Bundle extras = getIntent().getExtras();
if (extras == null) {
    return;
}
String strName = extras.getString("name");
String strTechnology = extras.getString("technology");

Still for your reference, here is the article to know more about the same: Android Intents

你怎么敢 2024-12-14 11:10:27

您必须传递来自活动 1 的文本并在活动 2 中将其作为捆绑包接收。

将 helloworld 程序作为您的第一个 Android 教程。

只需扩展 Activity 即可。您不需要提及超类的整个包。

You must pass the text from activity 1 and receive it as bundle in activity 2.

Go through the helloworld program as your first tutorial for android.

Just extending Activity will work. You do not need to mention the entire package of the superclass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文