我试图制作一个从Gridview项中选择值的简单游戏。但我希望按下播放按钮后可以点击该值。如果没有按播放按钮,我们无法点击任何地方。我怎么能这样做?
答案 0 :(得分:0)
您可以做的是更改网格视图的可点击属性 然后在按钮单击侦听器中将justable属性设置为true 所以你的代码应如下所示:
让我们假设这是您的观点:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="50dp" />
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" />
</LinearLayout>
您的活动应包含以下代码:
public class MainActivity extends AppCompatActivity {
private Button mButton;
private GridView mGridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView = (GridView) findViewById(R.id.grid);
mButton = (Button) findViewById(R.id.button);
// if you want to do it with setEnable
mGridView.setEnabled(false);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mGridView.setClickable(true);
// if you want to toggle then you can use the following code:
// mGridView.setClickable(!mGridView.isClickable());
// you can also change the setEnable property and not the clickable
mGridView.setEnabled(true);
}
});
}
}