在textviewclick上刷新webview?

时间:2014-10-05 14:44:57

标签: android webview textview android-webview

我的TextView下面有一个WebView。它工作得很好,但我需要的是,当有人点击textview时,基础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 个答案:

答案 0 :(得分: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);     
}

答案 1 :(得分:0)

在textview中添加以下行:

android:onClick="onClick"

然后在.java中:

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;
  }
}

希望这有帮助。