我试图将options_menu的onClickListener实现到另一个类但我无法弄清楚如何做到这一点目前代码中有两个我无法弄清楚的错误。第一个是
public class OptionsMenu implements View.OnClickListener{
,第二个是
PopupMenu popup = new PopupMenu(OptionsMenu.this, options_menu);
香港专业教育学院试图寻找一个解决方案,但我找不到一个或任何与代码有类似问题的人。我试图做的就是清理Home.Java,因此那里的代码更少。这也是我第一次尝试使用另一个课程,所以如果我能通过这个课程会有很多帮助,那么我可以开始一个新课程吗?
Home.Java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
public class Home extends AppCompatActivity {
private ImageButton options_menu;
private WebView webView;
private EditText search_bar;
private int on = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
webView = (WebView)findViewById(R.id.webView);
options_menu.findViewById(R.id.options_menu);
//Open in Equinox instead of Deafult Browser
webView.setWebViewClient(new
WebViewClient());
//WebView - JavaScript-WebViewSettings-HomePage
final WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.loadUrl("https://www.Google.ca");
//options button toolbar
options_menu = (ImageButton) findViewById(R.id.options_menu);
options_menu.findViewById(R.id.options_menu);
options_menu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popup = new PopupMenu(Home.this, options_menu);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.back:
if (webView.canGoBack()) {
webView.goBack();
}
else {
webView.reload();
}
return true;
case R.id.forward:
if (webView.canGoForward()) {
webView.goForward();
}
else {
webView.reload();
}
return true;
case R.id.refresh:
webView.reload();
default:
return false;
}
}
});
popup.show();
}
});
// settings for keyboard
search_bar = (EditText)findViewById(R.id.search_bar);
search_bar.setOnEditorActionListener(new OnEditorActionListener(){
@Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
if(arg1 == EditorInfo.IME_ACTION_GO)
{
if (search_bar.getText().toString().contains(".com")) {
webView.loadUrl("https://".concat(search_bar.getText().toString()));
} else {
webView.loadUrl("https://www.google.ca/search?q=".concat(search_bar.getText().toString()));
}
}
return false;
}
});
search_bar.requestFocus();
search_bar.setFocusableInTouchMode(true);
}
// Animation of search_bar
public void edit_clicked(View view) {
EditText search_bar = (EditText) findViewById(R.id.search_bar);
Animation anim = AnimationUtils.loadAnimation(Home.this, R.anim.edittext_scale);
search_bar.startAnimation(anim);
}
//Hard Back Button
public void onBackPressed(){
if (webView.canGoBack()){
webView.goBack();
}
else{
webView.reload();
}
}
}
OptionsMenu.Java
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.PopupMenu;
/**
* Created on 2018-01-05.
*/
public class OptionsMenu implements View.OnClickListener{
public ImageButton options_menu;
public WebView webView;
public void onclick (View view){
options_menu.findViewById(R.id.options_menu);
options_menu.setOnClickListener(new OnClickListener() {
public void onClick (View view){
PopupMenu popup = new PopupMenu(OptionsMenu.this, options_menu);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.back:
if (webView.canGoBack()) {
webView.goBack();
}
else {
webView.reload();
}
return true;
case R.id.forward:
if (webView.canGoForward()) {
webView.goForward();
}
else {
webView.reload();
}
return true;
case R.id.refresh:
webView.reload();
default:
return false;
}
}
});
popup.show();
}
});
}
}
答案 0 :(得分:0)
我无法真正解决您的问题,但我已经在您的示例中发现了一些问题。
第一:
PopupMenu
需要2个主要对象,第一个是Context
,您可以使用Activity
本身,第二个是应该附加PopupMenu的锚视图。
/* Main.this refers to the current `Main` activity object
and should be called from within the activity */
PopupMenu popupMenu = new PopupMenu(Main.this, imageButton);
第二:
您已经创建了OptionsMenu
课程,并且您已尝试从xml布局中进行充气。只有View
或扩展View
的对象可以从xml中膨胀(findViewById()
)而您的OptionsMenu
不是View
。
因此,这里说的是一个简单的例子,说明如何从xml中提取视图并将PopupMenu
附件移交给另一个类。
将PopupMenu附加到视图的类:
public class OptionsMenuHandler{
public OptionsMenuHandler(Context context, View anchorView) {
PopupMenu popupMenu = new PopupMenu(context, anchorView);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
return false;
}
});
}
}
活动:
public class TestActivity extends AppCompatActivity {
ImageButton menuButton;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.crop_image_view);
menuButton = findViewById(R.id.menuButton);
new OptionsMenuHandler(TestActivity.this, menuButton);
}
}
请记住,这只是为了教你如何使用课程。