[英]ANDROID - OnClick() Button Referencing Issue?
I would like to have my button in the OnClick() method start a new activity, however it does not do anything. 我想让我的按钮在OnClick()方法中启动一个新的活动,但是它什么也不做。 I think it is because the view that needs to be inflated would be the LinearLayout LoginLayout but I dont know how to reference it. 我认为这是因为需要放大的视图将是LinearLayout LoginLayout,但我不知道如何引用它。 The button works fine if I define it the way it is defined in the comment block. 如果按注释块中的定义方式定义按钮,则该按钮可以正常工作。
public class LoginActivity extends Fragment implements OnClickListener {
/*
* (non-Javadoc)
*
* @see
* android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
* android.view.ViewGroup, android.os.Bundle)
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
LinearLayout LoginLayout = (LinearLayout) inflater.inflate(
R.layout.activity_login, container, false);
/*
* Button btnLogin = (Button) LoginLayout.findViewById(R.id.btn_Login);
* btnLogin.setOnClickListener(new View.OnClickListener() {
*
* @Override public void onClick(View v) {
*
* Intent intent = new Intent(v.getContext(), Blankactivity.class);
* v.getContext().startActivity(intent);
*
*
* } });
*/
return LoginLayout;
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_Login) {
Intent intent = new Intent(v.getContext(), Blankactivity.class);
v.getContext().startActivity(intent);
startActivity(intent);
}
}
} }
Remove implements OnClickListener
since you are using annonymous inner class 因为使用匿名内部类,所以删除了implements OnClickListener
public class LoginActivity extends Fragment {
...
}
Also remove the below 同时删除以下内容
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_Login) {
Intent intent = new Intent(v.getContext(), Blankactivity.class);
v.getContext().startActivity(intent);
startActivity(intent);
}
You should use activity context instead of v.getContext(). 您应该使用活动上下文而不是v.getContext()。
http://developer.android.com/reference/android/app/Fragment.html#getActivity%28%29 http://developer.android.com/reference/android/app/Fragment.html#getActivity%28%29
Use getActivity()
to get the activity context. 使用getActivity()
获取活动上下文。
Button btnLogin = (Button) LoginLayout.findViewById(R.id.btn_Login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Blankactivity.class);
startActivity(intent);
}
});
I assume you use android:onclick for the click event in your layout? 我假设您使用android:onclick作为布局中的click事件?
If so then you don't need to use implements OnClickListener if you are in an activity. 如果是这样,那么如果您处于活动中,则无需使用工具OnClickListener。
if you use android:onclick="login" then the method public void login(View v) is called. 如果使用android:onclick =“ login”,则调用方法public void login(View v)。
If you try to use it in a fragment like here mine experience is that the android:onClick does not work. 如果您尝试在类似此处的片段中使用它,我的经验是android:onClick无法正常工作。
In that case do what Raghunandan says. 在这种情况下,Raghunandan会说。
or 要么
Button btnLogin = (Button) LoginLayout.findViewById(R.id.btn_Login);
btnLogin.setOnClickListener(this);
to use the button if you use implement onClickListener 如果使用工具onClickListener,则使用按钮
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.