自定义视图edittext调用了setontouchlistener,但未覆盖performclick

时间:2018-10-27 06:20:49

标签: java android android-studio

此问题的解决方案是什么?点击文本框(etPhoneNumber)时,应用强制关闭,请查看下面的android studio图片以了解错误。

自定义视图已调用setOnTouchListener,但未覆盖performClick如果覆盖onTouchEvent或使用OnTouchListener的视图也未实现performClick并在检测到点击时调用它,视图可能无法正确处理辅助功能。

理想情况下,应该将处理点击动作的逻辑放在View#performClick中,因为某些可访问性服务应在发生点击动作时调用performClick

https://imgur.com/a/JoaKO7E

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.ccp)
    CountryCodePicker ccp;
    @BindView(R.id.etPhoneNumber)
    EditText etPhoneNumber;
    @BindView(R.id.etMessage)
    EditText etMessage;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    @BindView(R.id.rlMainView)
    RelativeLayout rlMainView;

    ArrayList<Calldetails> calldetailsArrayList;
    private static final int MISSED_CALL_TYPE = 0;
    CallListAdapter callListAdapter;
    RuntimePermission runtimePermission;

    private static final String TAG = "MainActivity";
    private AdView mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(getString(R.string.app_name));

        runtimePermission = new RuntimePermission();
       /* Bitmap bmImg = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
        BitmapDrawable background = new BitmapDrawable(bmImg);
        background.setGravity(Gravity.);
        rlMainView.setBackgroundDrawable(background);*/


        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phone = etPhoneNumber.getText().toString().trim();
                String messge = etMessage.getText().toString().trim();
                String cc = ccp.getSelectedCountryCodeWithPlus();
                if (phone.length() < 10) {
                    Toast.makeText((Context) MainActivity.this, "Please add valid Phone number", Toast.LENGTH_SHORT).show();
                } else if (messge.length() <= 0) {
                    Toast.makeText((Context) MainActivity.this, "Please enter your message", Toast.LENGTH_SHORT).show();
                } else {
                    openWhatsApp(cc, phone, messge);
                }
            }
        });

        etPhoneNumber.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_LEFT = 0;
                final int DRAWABLE_TOP = 1;
                final int DRAWABLE_RIGHT = 2;
                final int DRAWABLE_BOTTOM = 3;

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (event.getRawX() >= (etPhoneNumber.getRight() - etPhoneNumber.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                        // your action here

                        runtimePermission.ask(MainActivity.this, new RuntimePermission.OnRequestPermissionsResultListener() {
                            @Override
                            public void onGranted(String permission[], int requestCode) {
                                FatchcallRecord();
                            }

                            @Override
                            public void onDenied(String permission[], int requestCode) {

                            }

                        },new String[]{Manifest.permission.READ_CALL_LOG}, 110, "Phone call permission need for view your call history","OK","CANCEL");

                        return true;
                    }
                }
                return false;
            }
        });

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        runtimePermission.onRequestPermissionsResult(requestCode,permissions,grantResults);
    }

    private void FatchcallRecord() {
        calldetailsArrayList = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy hh:mm:aa");
        Uri contacts = CallLog.Calls.CONTENT_URI;
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        Cursor managedCursor = getContentResolver().query(contacts, null, null, null, android.provider.CallLog.Calls.DATE + " DESC limit 200;");

        int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
        int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
        sb.append("Call Details :");

        try {
            if (managedCursor.moveToFirst()) {
                while (managedCursor.moveToNext()) {
                    Calldetails calldetails;
                    String phNumber = managedCursor.getString(number);
                    String callType = managedCursor.getString(type);
                    String callDate = managedCursor.getString(date);
                    Date callDayTime = new Date(Long.valueOf(callDate));
                    String callDuration = managedCursor.getString(duration);
                    String dir = null;
                    int dircode = Integer.parseInt(callType);
                    switch (dircode) {

                        case CallLog.Calls.OUTGOING_TYPE:
                            dir = "OUTGOING";
                            break;

                        case CallLog.Calls.INCOMING_TYPE:
                            dir = "INCOMING";
                            break;

                        case CallLog.Calls.MISSED_TYPE:
                            dir = "MISSED";
                            break;
                    }

                    calldetails = new Calldetails(phNumber,callType,formatter.format(callDayTime),callDuration);
                    calldetailsArrayList.add(calldetails);
                    sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
                            + dir + " \nCall Date:--- " + formatter.format(callDayTime)
                            + " \nCall duration in sec :--- " + callDuration);
                    sb.append("\n----------------------------------");
                }
            }
            managedCursor.close();
            showCallList();
        } catch (NumberFormatException e) {
                e.printStackTrace();
        }
        Log.i("###",sb.toString());
    }

    public void showCallList(){


        final Dialog dialogBuilder = new Dialog((Context) this);
        dialogBuilder.setCancelable(true);
        dialogBuilder.setContentView(R.layout.dialog_call_list);

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialogBuilder.getWindow().getAttributes());
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogBuilder.show();
        dialogBuilder.getWindow().setAttributes(lp);

        callListAdapter = new CallListAdapter(MainActivity.this,calldetailsArrayList,etPhoneNumber,dialogBuilder);
        RecyclerView recyclerView = (RecyclerView) dialogBuilder.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(callListAdapter);

        TextView tvCancle = (TextView) dialogBuilder.findViewById(R.id.tvCancle);
        tvCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogBuilder.dismiss();
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    //    https://mobikul.com/sending-message-application-whatsapp-number/
    public void openWhatsApp(String cc, String phone, String messge) {
        try {
            String toNumber = "" + cc + "" + phone; // Replace with mobile phone number without +Sign or leading zeros.


            Intent i = new Intent("android.intent.action.VIEW", Uri.parse("whatsapp://send/?text=" + URLEncoder.encode(messge, "UTF-8") + "&phone=" + toNumber));
//            Intent intent = new Intent(Intent.ACTION_VIEW);
//            intent.setData(Uri.parse("http://api.whatsapp.com/send?phone=" + toNumber + "&text=" + messge));
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

如果要检测触摸并单击分开,则应使用 SimpleGestureListner 不仅可以提供触摸事件,还可以提供onSingleClick。