How to open a website when a Button is clicked in Android application?

前端 未结 10 2063
囚心锁ツ
囚心锁ツ 2020-12-01 00:23

I am designing an app, with several button for users to click on. Once button is clicked, user is directed to appropriate website. How do I accomplish this?

10条回答
  •  一整个雨季
    2020-12-01 00:50

    public class MainActivity extends Activity {
    
    private WebView webView1;
    Button google; 
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        google = (Button) findViewById(R.id.google);
        google.setOnClickListener(new OnClickListener() {
    
            public void onClick(View arg0) {
                webView1 = (WebView) findViewById(R.id.webView);
                webView1.getSettings().setJavaScriptEnabled(true);
                webView1.loadUrl("http://www.google.co.in/");
    
          }
    });        
        }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    }
    

提交回复
热议问题