在 Android 应用程序中单击按钮时没有任何反应
Nothing happens when button is clicked in Android App
我无法让按钮在我正在处理的片段中工作。这是代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//create button listener for player match
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button mButton = (Button) rootView.findViewById(R.id.MatchButton);
Log.v("LOADING MATCH", "TEST");
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Log.v("LOADING MATCH", "TEST2");
Intent intent = new Intent(getActivity(), com.example.android.soccerstar.PlayerDisplay.class);
String message = "test";
intent.putExtra(intent.EXTRA_TEXT, message);
startActivity(intent);
}
});
return inflater.inflate(R.layout.fragment_main, container, false);
}
MatchButton 在片段 XML 中定义:
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold"
android:text="Match to Player"
android:id="@+id/MatchButton"/>
应用程序加载正常,但当我单击按钮时,没有任何反应。有任何想法吗?我对此还很陌生,所以请多多包涵。
谢谢!
将 onCreateView
的 return 更改为:
return rootView;
出现问题是因为为按钮和 onCreateView
中的 return 视图对象设置点击侦听器是不同的对象。
我无法让按钮在我正在处理的片段中工作。这是代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//create button listener for player match
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button mButton = (Button) rootView.findViewById(R.id.MatchButton);
Log.v("LOADING MATCH", "TEST");
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Log.v("LOADING MATCH", "TEST2");
Intent intent = new Intent(getActivity(), com.example.android.soccerstar.PlayerDisplay.class);
String message = "test";
intent.putExtra(intent.EXTRA_TEXT, message);
startActivity(intent);
}
});
return inflater.inflate(R.layout.fragment_main, container, false);
}
MatchButton 在片段 XML 中定义:
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold"
android:text="Match to Player"
android:id="@+id/MatchButton"/>
应用程序加载正常,但当我单击按钮时,没有任何反应。有任何想法吗?我对此还很陌生,所以请多多包涵。
谢谢!
将 onCreateView
的 return 更改为:
return rootView;
出现问题是因为为按钮和 onCreateView
中的 return 视图对象设置点击侦听器是不同的对象。