点击

时间:2017-03-05 20:43:57

标签: android android-coordinatorlayout floating-action-button

你能告诉我为什么我的FAB对点击没有反应吗?这是我的活动代码:

public class MainActivity extends AppCompatActivity {

WebView wView;
FloatingActionButton fab;
EditText source, target;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context = this;
    source = (EditText) findViewById(R.id.sourceAddr);
    target = (EditText) findViewById(R.id.targetAddr);

    wView = (WebView)findViewById(R.id.webView);
    fab = (FloatingActionButton ) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String sourceAddr = source.getText().toString();
            String targetAddr = source.getText().toString();

            if (sourceAddr.trim().length() > 0 && targetAddr.trim().length() > 0) {
                Toast.makeText(context, sourceAddr + ", "  +targetAddr, Toast.LENGTH_LONG);
            }
        }
    });
    WebSettings settings = wView.getSettings();
    settings.setJavaScriptEnabled(true);
    wView.loadUrl("file:////android_asset/web/index.html");
}

}

这是我的布局xml ...太长了所以我把它上传到了pastebin: http://pastebin.com/f3UfXGxa
我使用的是API 22。

1 个答案:

答案 0 :(得分:0)

这是解决方案:

 Toast.makeText(context, sourceAddr + ", "  +targetAddr, Toast.LENGTH_LONG).show();

你忘了调用show()方法。