[英]On button click switch to new layout xml file, using buttons now but how do I use fragments?
I am creating a Bluetooth app that requires a log in and/or sign up. 我正在创建一个需要登录和/或注册的蓝牙应用程序。 I use a button click to switch to new layout xml file, but I need to not use buttons and figure out how to use fragments!
我使用按钮单击来切换到新的布局xml文件,但是我不需要使用按钮并弄清楚如何使用片段! When I have a button on the second page that gets clicked it crashes the app.
当我在第二页上单击一个按钮时,它使应用程序崩溃。 Help :(
救命 :(
MainActivity.java 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()
should be in onCreate()
method within first few lines. setContentView()
应该在前几行中的onCreate()
方法中。 Otherwise it will not work. 否则它将无法正常工作。 If you need to change the layout after click on the button what you can do is, use
SharedPreferences
. 单击按钮后,如果需要更改布局,可以使用
SharedPreferences
。
Check the saved item in shared Preferences
before and then use setContentView()
method in onCreate()
. 之前在
shared Preferences
检查保存的项目,然后在onCreate()
使用setContentView()
方法。 Inside the button onClick ()
method save the wanted layout in SharedPreferences
. 在按钮的
onClick ()
方法内部,将所需的布局保存在SharedPreferences
。 Then write the code to restart the application. 然后编写代码以重新启动应用程序。
Check out this link: 查看此链接:
http://developer.android.com/guide/components/fragments.html http://developer.android.com/guide/components/fragments.html
And also check if your second activity is registered in the manifest.xml. 还要检查您的第二个活动是否已在manifest.xml中注册。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.