[英]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
。 然后編寫代碼以重新啟動應用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.