2012-12-08 91 views
0

用戶有一個帶有edittext和一個按鈕的活動。 在edittext中他們必須插入他們的登錄ID。然後按go鍵顯示其他活動,並使用webview加載正確的網站。輸入修改URL Webview?

一個例子。這是我希望用戶訪問的鏈接。 www.example.com/time/id= /類型=學生

如果sombody在的EditText進入2該宏將會打開時間學生ID 2等。

這就是我所擁有的。

LOGIN

package com.beter; 

import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.app.Activity; 
import android.content.Intent; 


public class LoginActivity extends Activity { 

Button go_btn; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.login); 

      go_btn = (Button) findViewById(R.id.go_btn); 




     go_btn.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent myIntent = new Intent(LoginActivity.this, WebActivity.class); 
      LoginActivity.this.startActivity(myIntent); 
     } 
    });  
} 

和佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<EditText 
    android:id="@+id/idfield" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:ems="10" 
    android:hint="@string/id" 
    android:inputType="number" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/go_btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/idfield" 
    android:text="@string/login" /> 

web視圖活動

package com.beter; 

import android.app.Activity; 
import android.os.Bundle; 
import android.webkit.WebView; 

public class WebActivity extends Activity { 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.webview); 


    WebView webview = new WebView(this); 
    setContentView(webview); 


    webview.loadUrl("http://roosters.gepro-osi.nl/roosters/rooster.php?wijzigingen=1&leerling=116277&type=Leerlingrooster&afdeling=12-13_OVERIG&school=905"); 
} 
} 

,再次佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <WebView 
     android:id="@+id/webView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

如何使用戶輸入。顯示良好的webview。 ?

回答

3

首先在你登錄活動得到你的EditText的引用作爲

go_btn = (Button) findViewById(R.id.go_btn); 
ed_txt = (EditText)findViewById(R.id.youredittextid); 

改變你的onClick到

public void onClick(View v) { 
    Intent myIntent = new Intent(LoginActivity.this, WebActivity.class); 
    myIntent.putExtra("id", ed_txt.getText()); 
    LoginActivity.this.startActivity(myIntent); 
} 

然後在你的WebActivity的onCreate retrive這個ID作爲

String id = getIntent().getExtras().getString("id"); 

然後你的網址將是

String myURL = "www.example.com/time/id="+id+"/type=student"; 
+0

它一直說id = null? – Georggroenendaal

+0

我的不好,把ed_txt.getText()改爲ed_txt.getText()。toString(); 您是否也在編輯文本中輸入了某些內容? –

+0

它現在正在工作。謝了哥們 ! – Georggroenendaal