开发者

Opening a new layout from a different layout?

开发者 https://www.devze.com 2023-04-02 16:01 出处:网络
I followed this tutorial (It\'s old, but I couldn\'t find any others; http://learnandroid.blogspot.com/2008/01/opening-new-screen-in-android.html)

I followed this tutorial (It's old, but I couldn't find any others; http://learnandroid.blogspot.com/2008/01/opening-new-screen-in-android.html)

So, I have it so that when I click on some text, it should open a different layout.

This is the XML code for the text I click on to open it:

<TextView android:textAppearance="?android:attr/textAppearanceSmall" 
android:layout_height="wrap_content" 
android:text="Not a member? Sign up now!" 
android:layout_width="wrap_content" 
android:id="@+id/signupText" 
android:layout_gravity="center"></TextView>
</LinearLayout>

And this is the Java coding for the Layout with the clickable text on it (The first "screen"):

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class NetworkActivity extends Activity {
public void onCreate(Bundle icicle)
   {
      super.onCreate(icicle);
      setContentView(R.layout.main);
      TextView click = (TextView) findViewById(R.id.signupText);
      click.setOnClickListener(new View.OnClickListen开发者_运维知识库er() {
          public void onClick(View arg0) {
          Intent i = new Intent(NetworkActivity.this, Signup.class);
          startActivity(i);
          } 
       });
    }
 }

The Java code for the second layout:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Signup extends Activity
{
public void onCreate(Bundle icicle)
{
  super.onCreate(icicle);
  setContentView(R.layout.signup);
  TextView b = (TextView) findViewById(R.id.signupText);
  b.setOnClickListener(new View.OnClickListener() {
     public void onClick(View arg0) {
     setResult(RESULT_OK);
     finish();
     } 
  });
 }
}

There's nothing special in the XML code for the second layout. (Should there be?)

When I click on the text in the Emulator, I get the error message that it's closed unexpectedly. How do I fix this?

Oh, and I saw other questions kind of like mine, but they weren't any help. I'm new to all this, so please don't get angry if I've done something "wrong". Thanks for the help in advance. =]


thy with this method it worked for me (here use button)

    View.OnClickListener handler = new View.OnClickListener(){



        public void onClick(View v) {
            switch (v.getId()) {

                case R.id.button1: 
                      setContentView(R.layout.layout1);
                      break;
                case R.id.button2: 

                    setContentView(R.layout.layout2);
                    break;
                case R.id.button3: 

                     setContentView(R.layout.layout3);
                     break;
                case R.id.button4: 

                    setContentView(R.layout.layout4);
                    break;

                  }
        }
   };
    findViewById(R.id.button1).setOnClickListener(handler);
    findViewById(R.id.button2).setOnClickListener(handler);
    findViewById(R.id.button3).setOnClickListener(handler);
    findViewById(R.id.button4).setOnClickListener(handler);


}


Most likely you didn't register your second activity in your android manifest. You need to create a <activity> tag for each one in your app. There should be an error in the logcat, basically saying "Could not find activity (Have you declared it in your manifest?)".

All you have to do to fix this, is add the tag inside your <application> tag:

<activity android:name=".Signup" android:label="@string/title_of_the_activity" />

See the <activity> tag documentation for further information.


we can do this by simply specifing android:clickable="true" android:onClick="name_of_method" in xml file of TextView.

0

精彩评论

暂无评论...
验证码 换一张
取 消