【问题标题】:Switching the layouts through Intents通过 Intent 切换布局
【发布时间】:2011-04-24 06:46:32
【问题描述】:

我有两个布局(xml 文件),我想从一个页面翻到另一个页面,这两个 xml 文件是 main.xml 和 register.xml,如果我单击 main.xml 中的登录按钮,页面应该会翻转并显示register.xml 和 register.xml 如果我单击提交按钮,它应该转到 main.xml 我尝试了很多活动,但我不这样做,因为我是 android 新手

请有人为我分享代码,

这是我的两个 xml 代码

main.xml

<LinearLayout
android:orientation="horizontal" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" android:background="@drawable/loginapp">

<RelativeLayout android:id="@+id/relativeLayout1" android:gravity="center" android:layout_marginTop="25dip" android:layout_height="177dip" android:background="@drawable/login_form_bg_green" android:layout_width="296dip">
 <EditText android:layout_marginRight="0dip"  android:id="@+id/userNameBox" android:layout_width="200px" android:background="@android:drawable/editbox_background" android:maxLines="1" android:layout_marginLeft="85dip" android:inputType="text" android:layout_height="wrap_content"></EditText>
 <EditText android:layout_marginRight="0dip"  android:id="@+id/passwordBox" android:layout_width="200px" android:background="@android:drawable/editbox_background" android:maxLines="1" android:layout_marginTop="45dip" android:layout_marginLeft="85dip" android:inputType="text|textVisiblePassword" android:layout_height="wrap_content"></EditText>
</RelativeLayout>

<LinearLayout android:id="@+id/ll_three" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@+id/ll_two" android:gravity="center">
  <Button 
    android:text="Sign In"
    android:id="@+id/Button01"
    android:layout_width="wrap_content" 
    android:layout_marginTop="5dip"
    android:layout_marginRight="15dip"
    android:layout_height="wrap_content"/>
<Button 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_marginTop="5dip"
    android:id="@+id/Button02"
    android:text="New user"/>

register.xml 是

<TableRow>
 <TextView android:id="@+id/TextView"   
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" REGISTER:"/>    
</TableRow>

<TableRow>
    <TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name:"/>
<EditText android:layout_marginRight="0dip"
  android:id="@+id/userNameBox" android:background="@android:drawable/editbox_background"
    android:maxLines="1" android:layout_marginLeft="15dip"   android:layout_weight="1"
    android:inputType="text" android:layout_height="35px" android:layout_width="0dip"></EditText>


</TableRow>

<TableRow>
<TextView android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name:"/>
<EditText android:layout_marginRight="0dip"
  android:id="@+id/userNameBox" android:layout_width="200px"  android:layout_weight="1" 
  android:background="@android:drawable/editbox_background" android:maxLines="1"
   android:layout_marginLeft="15dip" android:inputType="text" 
   android:layout_height="35px"></EditText>
</TableRow>


<TableRow>
    <TextView android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email:"/>
<EditText 
 android:id="@+id/userNameBox" android:layout_width="200px"
  android:background="@android:drawable/editbox_background"  android:layout_weight="1"
   android:maxLines="1" android:layout_marginLeft="15dip" android:layout_marginRight="0dip"  
   android:inputType="text" android:layout_height="35px">
   </EditText>
</TableRow>

<TableRow>
 <TextView android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile No:"/>
<EditText android:layout_marginRight="0dip" 
 android:id="@+id/userNameBox" android:layout_width="200px"   android:layout_weight="1"
  android:background="@android:drawable/editbox_background"
   android:maxLines="1" android:layout_marginLeft="15dip"
    android:inputType="text" android:layout_height="35px">
    </EditText>

</TableRow>   

这是我的 JAVA 文件

login.java

package com.android;


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

public class Login extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        });
    }
}




Register.java


package com.android;

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

public class Register extends Activity {

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        Button register = (Button) findViewById(R.id.Button03);
        register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }

        });
    }}

在调试项目时,这些是我发现的 DDMS 错误

Android [Android 应用程序]
DalvikVM[本地主机:8759]
线程 [ main](挂起(异常 RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) 行:2585
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) 行:2679
ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) 行:125 ActivityThread$H.handleMessage(Message) 行:2033
ActivityThread$H(Handler).dispatchMessage(Message) 行:99 Looper.loop() 行:123 ActivityThread.main(String[]) 行:4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) 行:不可用 [本机方法]
Method.invoke(Object, Object...) 行:521
ZygoteInit$MethodAndArgsCaller.run() 行:868
ZygoteInit.main(String[]) 行:626 NativeStart.main(String[]) 行:不可用 [本机方法]
线程 [ 活页夹线程 #2](正在运行) 线程 [ Binder 线程 #1](运行中

LOGCAT 显示这个

04-24 16:24:18.965: 错误/HierarchicalStateMachine(58): TetherMaster - unhandledMessage: msg.what=3 04-24 16:24:20.415: WARN/ActivityManager(58): HistoryRecord{44f2b188 com.android.launcher/com.android.launcher2.Launcher} 的活动空闲超时 04-24 16:24:22.965:WARN/WindowManager(58):应用程序冻结超时已过期。 04-24 16:24:22.965:WARN/WindowManager(58):强制清除冻结:AppWindowToken{450698d0 token=HistoryRecord{44f2b188 com.android.launcher/com.android.launcher2.Launcher}} 04-24 16:24:23.645:WARN/GoogleLoginService(180):设备没有帐户:发送 Intent { act=com.google.android.gsf.LOGIN_ACCOUNTS_MISSING } 04-24 16:24:31.296:WARN/PackageManager(58):pkg 的代码路径:com.android.cardioworld 从 /data/app/com.android.cardioworld-1.apk 更改为 /data/app/com。 android.cardioworld-2.apk 04-24 16:24:31.296:WARN/PackageManager(58):pkg 的资源路径:com.android.cardioworld 从 /data/app/com.android.cardioworld-1.apk 更改为 /data/app/com。 android.cardioworld-2.apk 04-24 16:24:34.216: WARN/RecognitionManagerService(58): 找不到可用的语音识别服务 04-24 16:24:36.214:WARN/SystemClock(123):无法将 rtc 设置为 1303642476:参数无效 04-24 16:24:41.174: WARN/ActivityThread(318): 应用程序 com.android.cardioworld 正在等待端口 8100 上的调试器... 04-24 16:24:50.686: WARN/ActivityManager(58): Launch timeout has expired,放弃唤醒锁! 04-24 16:24:51.170: WARN/ActivityManager(58): HistoryRecord{450caa98 com.android.cardioworld/.login}

的活动空闲超时

【问题讨论】:

标签: android android-layout


【解决方案1】:

如果您想更改布局,您必须在源文件夹中创建新的 Activity 类,并在创建时将内容视图设置为您想要的布局。另外不要忘记在应用程序内的清单文件中添加活动, 例如: 有很多方法可以做到这一点,但我喜欢这样做:

定义一个按钮 :Button btnSignIn; 然后在 Create 中设置布局并初始化按钮: btnSignIn =(按钮)findViewByid(R.id.btnSingIn); btnSignIn.setOnClickListener(this);

你会得到一个错误,但是把你的鼠标指针放在设置的onClick上......然后选择让'你的主ActivityClass'推动ClickListener。然后在主类上同样选择添加未实现的方法:这将添加一个设置 onClick(View v) 方法,在这里你可以做这样的事情:

  switch(v.getID()){

案例 R.id.btnSingIn: startActivity(new Intent(getBaseContext(), SignIn.class)); 休息; case R. ...//你可以继续。

但是我不知道你是否可以在不开始新活动的情况下更改布局。如果你发现 请回复:))。

希望对您有所帮助!

}

【讨论】:

    【解决方案2】:

    在Register.java上,我可以看到:

    Button next = (Button) findViewById(R.id.Button01);
    

    但在此之前,还有:

    setContentView(R.layout.register);
    

    但是 Button01 不在“注册”中,而是在 main 中。根本就没有注册按钮吧?

    【讨论】:

    • @Vincenta Plata 嗨,我再次修改了,如果可能,请再次查看,并且 button01 在 register.xml 文件中
    • 嗯不,它不是...至少不在您的编辑中。我猜 register.xml 有 TableLayout 作为它的根节点。 Button01 和 Button02 都在 main.xml LinearLayout 中(在另一个 LinearLayout 中)。
    • Soooo 只是为了澄清这一点:当然编译器不会给你任何错误,因为 Button01 和 Button02 存在。但是,由于 Button01 和 Button02 不在您的活动中,因此它们的 setOnClickListener 将无效。因为你不能点击它们。我的建议?以编程方式创建您的按钮,这样您就不会弄乱您想要获取的按钮是哪个。或者使用其他名称代替 Button01 和 Button02 和 ButtonX。也许是助记符?
    • @Vincenta Plata,实际上我现在已正确修改,但在保存编辑后它没有显示在主题中,在 main.xml 中我采用了 button01,button02。在 register.xml 我在最后添加了按钮 03 但它没有显示,你可以验证它并让我知道问题
    • 它还没有显示。请检查我之前的评论。我认为你只是对你的按钮感到困惑。 Java 代码理论上是可以的(当然,如果您在清单中声明了活动)。当您单击按钮时会发生什么?是错误还是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多