第一秒我制作了一个学生可以租房的Android应用程序。现在我在login en register页面上工作。
我的注册页面有问题。在我的应用程序中,有两种类型的用户注册,他们被称为"学生"和" Verhuurder"。在帐户片段中,寄存器页面有两个按钮。我无法让它工作到两页......
我在活动和片段方面遇到了很多麻烦......
任何人都可以帮助我吗?
我的代码:
帐户片段
public class AccountFragment extends Fragment implements View.OnClickListener{
public AccountFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (MainActivity.loginId == "")
{
return inflater.inflate(R.layout.fragment_account, container, false);
}
else
{
return inflater.inflate(R.layout.inlog_account, container, false);
}
}
// BUTTON
Button btnStudent = (Button) view.findViewById(R.id.btnRegisterStudent);
btnStudent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStudent:
//what to put here
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, new AccountFragmentStudent());
ft.commit();
break;
}
}
});
}
XML帐户片段
<Button
android:id="@+id/btnRegisterStudent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:text="Student op zoek naar een kamer"
android:background="@color/colorPrimary"
android:onClick="StudentRegister"/>
MainActivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, Callback<LoginResults> {
private EditText emailInput;
private EditText passwordInput;
private HomeFragment fragment;
public static String loginId = "";
public static String loginSecret = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HomeFragment fragment = new HomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
public void Login(View v) {
emailInput = (EditText) findViewById(R.id.email);
passwordInput = (EditText) findViewById(R.id.password);
String email = emailInput.getText().toString();
String password = passwordInput.getText().toString();
doLogin(email, password);
}
public void onResponse(Response<LoginResults> response) {
if (response.isSuccess() && response.body() != null) {
loginId = response.body().clientId;
loginSecret = response.body().clientSecret;
fragment.setLoginToken1(loginId);
fragment.setLoginToken2(loginSecret);
new AlertDialog.Builder(MainActivity.this)
.setTitle("Gelukt")
.setMessage("U bent ingelogd")
.setCancelable(false)
.setPositiveButton("oke", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create().show();
// Create fragment and give it an argument specifying the article it should show
InlogAccountFragment newFragment = new InlogAccountFragment();
Bundle args = new Bundle();
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
else
new AlertDialog.Builder(MainActivity.this)
.setTitle("Mislukt")
.setMessage("Uw inloggegevens zijn incorrect")
.setCancelable(false)
.setPositiveButton("oke", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create().show();
{
}
}
public void onFailure(Throwable t) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Er is iets fouts gegaan")
.setMessage("Probeer opnieuw")
.setCancelable(false)
.setPositiveButton("oke", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create().show();
}
public void doLogin(String email, String password){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("BLABLA")
.addConverterFactory(GsonConverterFactory.create())
.build();
Login service = retrofit.create(Login.class);
service.loginResults(
email,
password
).enqueue(this);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
HomeFragment fragment = new HomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_favorieten) {
FavoriteFragment fragment = new FavoriteFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_berichtenbox) {
MessageFragment fragment = new MessageFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_account) {
AccountFragment fragment = new AccountFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_instellingen) {
SettingsFragment fragment = new SettingsFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:1)
我通常会尝试使我的碎片保持光亮和分离,并将重物(特别是与其他碎片的相互作用)委托给正在使用它们的活动。您可能想尝试以下方法:
尝试向AccountFragment
添加一个侦听器界面 - 类似这样的内容:
public interface Listener {
void onStudentRegistrationSelected();
void onVerhuurderRegistrationSelected();
}
您还需要添加:
// Member declaration at the top of the fragment class
private Listener mListener;
@Override public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (Listener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement " +
Listener.class.getSimpleName() + ".");
}
}
这将设置使用片段作为侦听器的活动。
然后,在AccountFragment
中设置相应按钮的on click处理程序,以在侦听器上调用相应的方法。像这样:
view.findViewById(R.id.btnRegisterStudent).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onStudentRegistrationSelected();
}
}
返回MainActivity
,实现AccountFragment.Listener
接口,覆盖侦听器的方法,并在每个侦听器方法覆盖中创建并提交适当的注册片段 - 例如{{1}将显示onStudentRegistrationSelected()
片段。此外,如果您将其添加到片段backstack中,那么如果用户在AccountFragmentStudent
上点击后退按钮,则用户将返回AccountFragment
,这是一个很好的用户体验。
希望有所帮助!