gpt4 book ai didi

java - onKeyDown 事件的范围变量问题

转载 作者:行者123 更新时间:2023-12-02 00:40:01 26 4
gpt4 key购买 nike

我在 if 语句中使用 var "str"时遇到问题。我知道这是一个范围问题,但我不知道如何解决它。我已经失败了好几次了。

我的目标是在 if 语句中使用“str”的值来显示或不显示警报。限制是我只能以这种方式分配“str”的值。

 public class MyJavaScriptInterface  
{
public String showHTML(String html)
{
String str = html;
return str;
}
}



@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{

//Handle the back button
if(keyCode == KeyEvent.KEYCODE_BACK && str =="0")
{
//Ask the user if they want to quit

更新:

好吧,我尽量不打扰您阅读整个代码,但它很短,可能会有所帮助。

在 web View 中,我显示了 html 页面,并且还读取了一个隐藏变量,该变量将决定是否应显示警报框。我只是想将该值传递给 keydown 推送中的 if 语句。这是完整的代码。

package com.ishop.pizzaoven;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class buttonOne extends Activity
{
WebView wb = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.buttons);
wb = new WebView(this);
wb.getSettings().setJavaScriptEnabled(true);
wb.setWebViewClient(new HelloWebViewClient());
wb.getSettings().setJavaScriptEnabled(true);
wb.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
/* WebViewClient must be set BEFORE calling loadUrl! */
wb.setWebViewClient(new WebViewClient()
{
@Override
public void onPageFinished(WebView view, String url)
{
/* This call inject JavaScript into the page which just finished loading. */
wb.loadUrl("javascript:window.HTMLOUT.showHTML(document.getElementById('sendtextcoupon').value);");
}
});
wb.loadUrl("http://ishopstark.com/mobileapp.php?category=1");
setContentView(wb);
}

private class HelloWebViewClient extends WebViewClient
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}

final Context myApp = this;

public class MyJavaScriptInterface
{
public void showHTML(String html)
{
String str = html;
}
}




public boolean onKeyDown(int keyCode, KeyEvent event)
{

//Handle the back button
if(keyCode == KeyEvent.KEYCODE_BACK)
{
//Ask the user if they want to quit
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Coupon")
.setMessage("Do you want a coupon texted to you?")
.setPositiveButton("YES!", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//Stop the activity
Toast.makeText(buttonOne.this, "Great! You'll get one in just a couple of minutes.", Toast.LENGTH_LONG).show();
finish();
}
})
.setNegativeButton("Not now", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//Stop the activity
finish();
}
})
.show();
return true;
}
else
{
return super.onKeyDown(keyCode, event);
}

}

}// End of main class

更新 2:

好的,所以我稍微改变了我的代码。但现在我收到警报错误有什么想法吗? “方法 setIcon(int) 未定义类型为buttonOne.MyJavaScriptInterface”我在 .setIcon(android.R.drawable.ic_dialog_alert) 上收到此错误并返回 super.onKeyDown(keyCode, event);

public class MyJavaScriptInterface  
{
public String showHTML(String html)
{
String str = html;
return str;
}
public boolean onKeyDown(int keyCode, KeyEvent event, String str)
{

//Handle the back button
if(keyCode == KeyEvent.KEYCODE_BACK && str == "0")
{
//Ask the user if they want to quit
AlertDialog.Builder alertbox = new AlertDialog.Builder(buttonOne.this);
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Coupon")
.setMessage("Do you want a coupon texted to you?")
.setPositiveButton("YES!", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//Stop the activity
Toast.makeText(buttonOne.this, "Great! You'll get one in just a couple of minutes.", Toast.LENGTH_LONG).show();
finish();
}
})
.setNegativeButton("Not now", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//Stop the activity
finish();
}
})
.show();
return true;
}
else
{
return super.onKeyDown(keyCode, event);
}

}
}

最佳答案

不确定您的具体目标是什么,但是您不能将字符串设为成员变量吗?

public class MyJavaScriptInterface  
{
private String str;
public String showHTML(String html)
{
str = html;
return str;
}
}

关于java - onKeyDown 事件的范围变量问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6654301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com