ontouchlistener代码不能应用于int

时间:2015-01-26 17:40:25

标签: android this ontouchlistener

我在android studio上构建一个android应用程序,我在命令ontouchlistener代码上放置if语句,并在“this”上显示消息问题,我认为因为它需要int而不是意图?所以我需要解决它:)

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    logpassj = (TextView) findViewById(R.id.logpass);
    logpassjH = (TextView) findViewById(R.id.logpassH);
    mPasswordView = (EditText) findViewById(R.id.password);
    mSearchView = (SearchView) findViewById(R.id.search_view);
logpassj.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                logpassjH.setVisibility(View.VISIBLE);
                return true;
            }
            if (event.getAction()== MotionEvent.ACTION_DOWN)
            {
                logpassj.setVisibility(View.INVISIBLE);
                if(mPasswordView.getText().toString().equals("123456789"))
                {
                    Intent intent = new Intent(getApplicationContext(),ITCutiesReaderAppActivity.class);
                    startActivity(intent);
                    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
                    Notification noti = new Notification.Builder(this)
                            .setTicker("Calcupital")
                            .setContentTitle("Calcupital")
                            .setContentText("User Information has been updated successfully")
                            .setSmallIcon(R.drawable.ic_launcher4)
                            .setContentIntent(pIntent).getNotification();
                    noti.flags=Notification.FLAG_AUTO_CANCEL;
                    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    notificationManager.notify(0, noti);
                    mPasswordView.setText("");



                }
                else
                {
                    Toast.makeText(getApplicationContext(),
                            "Invalid ID Information", Toast.LENGTH_LONG).show();
                    mPasswordView.setText("");

                }

            }
            return true;
        }
    });

}

3 个答案:

答案 0 :(得分:1)


更改
this


MainActivity.this


在这里,
 MainActivity - >您的活动名称

答案 1 :(得分:0)

您的问题是'this'是指OnTouchListener而不是活动。

您需要将该if语句的内容移动到私有函数

if (event.getAction()== MotionEvent.ACTION_DOWN)
        { myFunction();   ...

private void myFunction(){
   // create your intent here
   // 'this' will now refer to the activity

答案 2 :(得分:0)

您需要从Context方法中将“外部”作为有效OnTouchListener对象提供,因为在OnTouchListener 此的类型为OnTouchListener

将您的代码更改为:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    logpassj = (TextView) findViewById(R.id.logpass);
    logpassjH = (TextView) findViewById(R.id.logpassH);
    mPasswordView = (EditText) findViewById(R.id.password);
    mSearchView = (SearchView) findViewById(R.id.search_view);
logpassj.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                logpassjH.setVisibility(View.VISIBLE);
                return true;
            }
            if (event.getAction()== MotionEvent.ACTION_DOWN)
            {
                logpassj.setVisibility(View.INVISIBLE);
                if(mPasswordView.getText().toString().equals("123456789"))
                {
                    Intent intent = new Intent(getApplicationContext(),ITCutiesReaderAppActivity.class);
                    startActivity(intent);
                    PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
                    Notification noti = new Notification.Builder(MainActivity.this)
                            .setTicker("Calcupital")
                            .setContentTitle("Calcupital")
                            .setContentText("User Information has been updated successfully")
                            .setSmallIcon(R.drawable.ic_launcher4)
                            .setContentIntent(pIntent).getNotification();
                    noti.flags=Notification.FLAG_AUTO_CANCEL;
                    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    notificationManager.notify(0, noti);
                    mPasswordView.setText("");



                }
                else
                {
                    Toast.makeText(getApplicationContext(),
                            "Invalid ID Information", Toast.LENGTH_LONG).show();
                    mPasswordView.setText("");

                }

            }
            return true;
        }
    });

}