2014-10-05 55 views
0

我有這TextView與它下面WebView。它工作正常,但我需要的是,當有人點擊textview時底層WebView被刷新。在textviewclick上刷新webview?

這裏是我的.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:ads="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MyActivity" 
android:background="#ffffff" 
android:clickable="false"> 


<TextView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/vu" 
android:id="@+id/textView" 
android:gravity="center" 
android:textSize="60sp" 
android:textColor="@color/text_color" 
android:background="#212121" 
android:clickable="true" 
android:layout_alignParentTop="true" 
android:layout_alignParentLeft="true" 
android:layout_alignParentStart="true" 
android:layout_alignParentRight="true" 
android:layout_alignParentEnd="true" /> 

<WebView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/webView" 
    android:layout_below="@+id/textView" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentBottom="true" /> 

</RelativeLayout> 

和我的.java:

public class MyActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 

    TextView tv = (TextView) findViewById(R.id.textView); 
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Light.ttf"); 
    tv.setTypeface(typeface); 
    WebView webView = (WebView) findViewById(R.id.webView); 
    webView.loadUrl("http://www.papery.hol.es"); 
    webView.setWebViewClient(new WebViewClient()); 
    webView.setVerticalScrollBarEnabled(false); 
    webView.setOverScrollMode(View.OVER_SCROLL_NEVER);  
    } 
} 
+2

setOnClick方法爲textView,並把你的webview代碼在裏面。 – Chitrang 2014-10-05 14:50:50

+0

對不起,但我知道你的意思,只是不知道如何在實際代碼中輸入它......你能編輯我的代碼嗎? :) – CristianoWilson 2014-10-05 14:55:51

回答

1
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 

    TextView tv = (TextView) findViewById(R.id.textView); 
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Light.ttf"); 
    tv.setTypeface(typeface); 

    tv.setOnClickListener(new OnClickListener(){ 
     public void onClick(View view) { 
      Toast.makeText(MainActivity.this, "TextView Clicked", Toast.LENGTH_SHORT).show(); 
      // Every Time the textview clicked 
      webViewWork(); 
     } 
    });   

    // First Time 
    webViewWork(); 

    } 
} 

void webViewWork(){ 
    WebView webView = (WebView) findViewById(R.id.webView); 
    webView.loadUrl("http://www.papery.hol.es"); 
    webView.setWebViewClient(new WebViewClient()); 
    webView.setVerticalScrollBarEnabled(false); 
    webView.setOverScrollMode(View.OVER_SCROLL_NEVER);  
} 
0

加入這一行你的TextView:

中的.java
android:onClick="onClick" 

然後:

public void onClick(View v){ 
    switch (v.getId()) { 
     case R.id.textView: 
     webView.loadUrl("http://www.papery.hol.es"); // This line is just a representation 
       break; 
      default: 
       break; 
    } 
} 

希望這會有所幫助。

+1

http://gyazo.com/e262739f9db613730572169fa128bdbc – CristianoWilson 2014-10-05 15:01:10