OnClick列表器不起作用

时间:2018-08-06 11:22:59

标签: android

不知道为什么当我单击按钮时它不执行任何操作。无法在onclick列表器中找到任何错误。它应该为我提供选择图像的选项,当我单击选择按钮时而不是单击时在“选择”按钮上,它不执行任何操作,也不会给出任何错误。

Java代码:

package com.vshine.neuron;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.vshine.neuron.testing.R;

import org.w3c.dom.Text;

import java.io.IOException;

public class imageupload extends AppCompatActivity implements View.OnClickListener {
Button btn1 , btn2;
TextView tv;
EditText edt;
ImageView img;
private final static int IMG_REQUEST =1;
private Bitmap bitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imageupload);

        btn1 = findViewById(R.id.btnupload);
        btn2 = findViewById(R.id.btnchose);
        //tv = findViewById(R.id.textview);
        edt = findViewById(R.id.edit);
        img = findViewById(R.id.imgview);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {

            case R.id.btnchose:

                break;
        }
    }

    private void selectImage()
    {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent,IMG_REQUEST);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==IMG_REQUEST && resultCode==RESULT_OK && data !=null)
        {
            Uri path = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),path);
                img.setImageBitmap(bitmap);
                img.setVisibility(View.VISIBLE);
                edt.setVisibility(View.VISIBLE);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

这是布局的XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.vshine.neuron.imageupload">

    <!--<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="lang and lat"
        android:textSize="20dp"
        android:id="@+id/textview"/>-->
    <ImageView
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:id="@+id/imgview"
        android:visibility="gone"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter a Name"
        android:visibility="gone"
        android:id="@+id/edit"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Upload"
        android:id="@+id/btnupload"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choose image"
        android:id="@+id/btnchose"/>

</LinearLayout>

5 个答案:

答案 0 :(得分:5)

检查此。如果Toast工作正常,则说明您已正确实现了侦听器

@Override
public void onClick(View v) {
switch(v.getId())
{
 case R.id.btnchose:
       Toast.makeText(this, "Caleed or not", Toast.LENGTH_SHORT).show();     
 }
}

答案 1 :(得分:3)

您忘记在selectImage()方法中调用您的onClick()方法

 @Override
    public void onClick(View v) {

        switch (v.getId())
        {

            case R.id.btnchose:
                selectImage();
                break;
        }
    }

答案 2 :(得分:2)

尝试一下!

package com.vshine.neuron;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.vshine.neuron.testing.R;

import org.w3c.dom.Text;

import java.io.IOException;

public class imageupload extends AppCompatActivity implements View.OnClickListener {
Button btn1 , btn2;
TextView tv;
EditText edt;
ImageView img;
private final static int IMG_REQUEST =1;
private Bitmap bitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imageupload);

        btn1 = findViewById(R.id.btnupload);
        btn2 = findViewById(R.id.btnchose);
        //tv = findViewById(R.id.textview);
        edt = findViewById(R.id.edit);
        img = findViewById(R.id.imgview);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {

            case R.id.btnchose:
                   selectImage();
             //do what you want
                break;
        }
    }

    private void selectImage()
    {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent,IMG_REQUEST);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==IMG_REQUEST && resultCode==RESULT_OK && data !=null)
        {
            Uri path = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),path);
                img.setImageBitmap(bitmap);
                img.setVisibility(View.VISIBLE);
                edt.setVisibility(View.VISIBLE);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

答案 3 :(得分:2)

将selectImage添加到onClick方法中,如下所示:

@Override
public void onClick(View v) {

    switch (v.getId())
    {

        case R.id.btnchose:
            selectImage();
            break;
    }
}

答案 4 :(得分:1)

在您的代码中调用selectImage()方法:

switch (v.getId())
    {

        case R.id.btnchose:
            selectImage();
            break;
    }