繁体   English   中英

在按钮上单击,切换到新的布局xml文件,现在使用按钮,但是如何使用片段?

[英]On button click switch to new layout xml file, using buttons now but how do I use fragments?

我正在创建一个需要登录和/或注册的蓝牙应用程序。 我使用按钮单击来切换到新的布局xml文件,但是我不需要使用按钮并弄清楚如何使用片段! 当我在第二页上单击一个按钮时,它使应用程序崩溃。 救命 :(

MainActivity.java

package com.example.chirp;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.bluetooth.BluetoothAdapter;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter() ;
        bluetooth.setName("Chirp Bluetooth");

        String status; {
            if (bluetooth != null)
            {

                if (bluetooth.isEnabled())
                {
                    String mydeviceaddress = bluetooth.getAddress();
                    String mydevicename = bluetooth.getName();
//                  String state = bluetooth.getState();
//                  status = mydevicename + ":" + mydeviceaddress + ":" + state;
                }
                else
                {
                    status = "Bluetooth is not Enabled.";
                }
                }
//          Toast.makeText(this, status, Toast.LENGTH_LONG).show();
            }   

//        Configuration config = getResources().getConfiguration();
//
//        FragmentManager fragmentManager = getFragmentManager();
//        FragmentTransaction fragmentTransaction = 
//        fragmentManager.beginTransaction();
//       
        Button quickfindbutton = (Button) findViewById(R.id.qfbutton);
        quickfindbutton.setOnClickListener(new OnClickListener(){
            public void onClick(View v){

            }
        });

        Button loginbutton = (Button) findViewById(R.id.button1);
        loginbutton.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                setContentView(R.layout.loginscreen);               

        };

        });


    Button signupbutton = (Button) findViewById(R.id.button2);
    signupbutton.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            setContentView(R.layout.signupscreen);
        }
         });

//    Button login = (Button) findViewById(R.id.login);
//    login.setOnClickListener(new OnClickListener(){
//      public void onClick(View v){
//          setContentView(R.layout.devicepage);
//      }
//       });




    }

    public void showToast(String message) {

        Toast toast = Toast.makeText(getApplicationContext(), message,
                Toast.LENGTH_SHORT);

        toast.show();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
           setContentView(R.layout.activity_main);
           return true;

        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true; 
    }

//    public void newMessage(View v){
//      Intent intent = new Intent(this,loginscreen.class);
//      startActivity(intent);
//    }
    }

setContentView()应该在前几行中的onCreate()方法中。 否则它将无法正常工作。 单击按钮后,如果需要更改布局,可以使用SharedPreferences

之前在shared Preferences检查保存的项目,然后在onCreate()使用setContentView()方法。 在按钮的onClick ()方法内部,将所需的布局保存在SharedPreferences 然后编写代码以重新启动应用程序。

查看此链接:

http://developer.android.com/guide/components/fragments.html

还要检查您的第二个活动是否已在manifest.xml中注册。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM