【问题标题】:Why am I not able to press the buttons when I come into the page?为什么我进入页面时无法按下按钮?
【发布时间】:2013-12-09 05:42:51
【问题描述】:
public class Uploads extends Activity implements OnClickListener {

    ImageView btnAllShops, btnFavourites, btnUploads, btnSettings, btnBuys,
            btnTakePhoto;

    GridView gvUploads;

    PhotoDbAdapter ourHelper;

    private String description, category, price, imagepath;

    final static int cameraData = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_uploads);
        findViewById();
        onBackPressed();
        sdcard();

    }

    private void findViewById() {
        gvUploads = (GridView) findViewById(R.id.gvUploads);
        btnAllShops = (ImageView) findViewById(R.id.btnAllShops);
        btnFavourites = (ImageView) findViewById(R.id.btnFavourites);
        btnUploads = (ImageView) findViewById(R.id.btnUploads);
        btnSettings = (ImageView) findViewById(R.id.btnSettings);
        btnBuys = (ImageView) findViewById(R.id.btnBuys);
        btnTakePhoto = (ImageView) findViewById(R.id.btnTakePhoto);

        btnAllShops.setOnClickListener(this);
        btnFavourites.setOnClickListener(this);
        btnUploads.setOnClickListener(this);
        btnSettings.setOnClickListener(this);
        btnBuys.setOnClickListener(this);
        btnTakePhoto.setOnClickListener(this);

    }

    @Override
    public void onBackPressed() {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap bmp = (Bitmap) extras.get("data");

            Intent goMain = new Intent(getApplicationContext(),
                    EditUploads.class);

            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, bs);
            goMain.putExtra("byteArray", bs.toByteArray());
            startActivity(goMain);
        }
    }

    private void sdcard() {
        setContentView(R.layout.activity_uploads);

        // Set up an array of the Thumbnail Image ID column we want
        String[] projection = { MediaStore.Images.Thumbnails._ID };
        // Create the cursor pointing to the SDCard
        cursor = managedQuery(
                MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, // Which
                                                                                // columns
                                                                                // to
                                                                                // return
                null, // Return all rows
                null, MediaStore.Images.Thumbnails.IMAGE_ID);
        // Get the column index of the Thumbnails Image ID
        columnIndex = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

        GridView gvuploads = (GridView) findViewById(R.id.gvUploads);
        gvuploads.setAdapter(new ImageAdapter(this));

    }

    private class ImageAdapter extends BaseAdapter {

        private Context context;

        public ImageAdapter(Context localContext) {
            context = localContext;
        }

        public int getCount() {
            return cursor.getCount();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView picturesView;
            if (convertView == null) {
                picturesView = new ImageView(context);
                // Move cursor to current position
                cursor.moveToPosition(position);
                // Get the current value for the requested column
                int imageID = cursor.getInt(columnIndex);
                // Set the content of the image based on the provided URI
                picturesView.setImageURI(Uri.withAppendedPath(
                        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
                                + imageID));
                picturesView.setScaleType(ImageView.ScaleType.FIT_XY);
                picturesView.setPadding(10, 10, 10, 10);
                picturesView
                        .setLayoutParams(new GridView.LayoutParams(266, 266));
            } else {
                picturesView = (ImageView) convertView;
            }
            return picturesView;
        }
    }

    private Cursor cursor;

    // Column index for the Thumbnails Image IDs.

    private int columnIndex;

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
        case R.id.btnAllShops:
            Intent iA = new Intent(getApplicationContext(), AllShops.class);
            startActivity(iA);
            break;

        case R.id.btnFavourites:
            Intent iF = new Intent(getApplicationContext(), Favourites.class);
            startActivity(iF);

            break;

        case R.id.btnUploads:
            Intent iU = new Intent(getApplicationContext(), Uploads.class);
            startActivity(iU);

            break;

        case R.id.btnSettings:
            Intent iS = new Intent(getApplicationContext(),
                    SettingsActivity.class);
            startActivity(iS);

            break;

        case R.id.btnBuys:
            Intent iBuy = new Intent(getApplicationContext(), Buys.class);
            startActivity(iBuy);

            break;

        case R.id.btnTakePhoto:
            Intent i = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, cameraData);
            break;
        }
    }
}

在添加网格视图布局之前,我能够使用按钮功能。但是,在我插入网格视图布局后,我所能做的就是滚动浏览网格视图上的图像。我该怎么做才能既可以滚动图像又可以使用按钮?谢谢

公共类 Mobile 扩展 Activity {

PhotoDbAdapter ourHelper;
ImageView grid_item_image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.mobile);

    grid_item_image = (ImageView) findViewById(R.id.grid_item_image);
    PhotoDbAdapter getImage = new PhotoDbAdapter(this);
    getImage.open();
    String getImagepathe = getImage.getImagePath();

    Toast.makeText(this, getImagepathe, Toast.LENGTH_LONG).show();

    int id = getResources().getIdentifier(getImagepathe, null, null);

    grid_item_image.setImageResource(id);

    Intent mobile = new Intent(getApplicationContext(), Uploads.class);

    startActivity(mobile);

}

}

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="true"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:src="@drawable/sh_uploads" />

    <ImageView
        android:id="@+id/btnTakePhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/hbtn_camera" />
</LinearLayout>

<GridView
    android:id="@+id/gvUploads"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/bottomBar"
    android:layout_below="@+id/linearLayout1"
    android:numColumns="3"
    tools:listitem="@layout/mobile" >
</GridView>

<LinearLayout
    android:id="@+id/bottomBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/btnAllShops"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.0"
        android:contentDescription="@null"
        android:src="@drawable/sbtn_home" />

    <ImageView
        android:id="@+id/btnFavourites"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.0"
        android:contentDescription="@null"
        android:src="@drawable/sbtn_fave" />

    <ImageView
        android:id="@+id/btnBuys"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.0"
        android:contentDescription="@null"
        android:src="@drawable/sbtn_buys" />

    <ImageView
        android:id="@+id/btnUploads"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.0"
        android:contentDescription="@null"
        android:src="@drawable/sbtn_uploads" />

    <ImageView
        android:id="@+id/btnSettings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1.0"
        android:contentDescription="@null"
        android:src="@drawable/sbtn_settings" />
</LinearLayout>

【问题讨论】:

  • 是否将布局包含在另一个中,否则可能存在上下文问题。请为此发布更多代码。
  • 这是另一个也有 gridview 布局的类。如何检查上下文是否有问题?
  • 调试你的代码,想知道你的代码在哪里停止。
  • 刚刚尝试过,但不知何故没有显示错误
  • 发布包含网格视图和按钮的 xml 布局

标签: java android button gridview


【解决方案1】:

如果图像进入网格,也许这会有所帮助:

gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { if(v.getId() == btnFavourites.getId()){ //Something happen } } });

【讨论】:

  • 如果你能进一步解释它是如何工作的以及我想把这行代码大致放在哪里可以吗?对不起..我对这些东西很陌生..提前谢谢
  • 对不起,我是新使用 StackOverflow,但你可以试试这个:pastebin.com/raw.php?i=RmwCXpeA
  • 当我运行应用程序时,当我点击页面时它会崩溃。我检查了 logcat,它说使用 setonItemclicklistener 代替。这是个好主意吗?
  • 用你的方法试过了,但是还是不行。。还是非常感谢
猜你喜欢
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
  • 2022-01-25
相关资源
最近更新 更多