ontouchlistener 代码不能应用于 int
ontouchlistener code cant be applied on int
我在 android studio 上构建了一个 android 应用程序,我将 if 语句放在命令 ontouchlistener 代码上,并在 "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;
}
});
}
改变
this
到
MainActivity.this
这里,
MainActivity -> 你的 Activity 姓名
您的问题是 'this' 指的是 OnTouchListener 而不是 activity。
您需要将该 if 语句的内容移至私有函数
if (event.getAction()== MotionEvent.ACTION_DOWN)
{ myFunction(); ...
private void myFunction(){
// create your intent here
// 'this' will now refer to the activity
您需要从 OnTouchListener
方法中将 "outer this" 引用为有效的 Context
对象,因为在 OnTouchListener
类型的对象内部=12=] this 的类型是 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;
}
});
}
我在 android studio 上构建了一个 android 应用程序,我将 if 语句放在命令 ontouchlistener 代码上,并在 "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;
}
});
}
改变
this
到
MainActivity.this
这里,
MainActivity -> 你的 Activity 姓名
您的问题是 'this' 指的是 OnTouchListener 而不是 activity。
您需要将该 if 语句的内容移至私有函数
if (event.getAction()== MotionEvent.ACTION_DOWN)
{ myFunction(); ...
private void myFunction(){
// create your intent here
// 'this' will now refer to the activity
您需要从 OnTouchListener
方法中将 "outer this" 引用为有效的 Context
对象,因为在 OnTouchListener
类型的对象内部=12=] this 的类型是 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;
}
});
}