2013-10-09 68 views
0

我有一個XML充氣自定義視圖類。它包括按鈕和webview。我將這個視圖類添加到主要活動(VividViewerActivity)中。第一次我將可見性設置爲隱形。在這個主要活動中有一個方法(loadPrintActivity),我將視圖類的可見性設置爲可見。一切正常。但觸摸不適合我。這裏是我的課:觸摸事件不適用於上視圖

public class PrintView extends RelativeLayout { 
Button btnClose,btnPrint; 
WebView wev; 
public PrintView(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
    View.inflate(context, R.layout.printscreen, this); 
    wev = (WebView) findViewById(R.id.webViewPrint); 
    btnClose = (Button)findViewById(R.id.btnClose); 
    btnPrint = (Button)findViewById(R.id.btnPrint); 
    btnClose.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Log.d("VIEWWWWWWWWWWWWWWWWWWWWW", "kaj korce.............."); 
      btnPrint.setVisibility(View.INVISIBLE); 
     } 
    }); 
} 


@Override 
public boolean onTouchEvent(MotionEvent event) { 
    // TODO Auto-generated method stub 
    return super.onTouchEvent(event); 
} 


@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    // TODO Auto-generated method stub 
    for(int i = 0 ; i < getChildCount() ; i++){ 
     getChildAt(i).layout(l, t, r, b); 
     } 
} 

這裏是我的XML佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/top" 
     android:gravity="center" > 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <Button 
       android:id="@+id/btnClose" 
       android:layout_width="80dp" 
       android:layout_height="30dp" 
       android:layout_alignParentLeft="true" 
       android:layout_marginLeft="10dp" 
       android:background="@drawable/btn" 
       android:text="Close" /> 

      <Button 
       android:id="@+id/btnPrint" 
       android:layout_width="80dp" 
       android:layout_height="30dp" 
       android:layout_alignParentRight="true" 
       android:layout_marginRight="10dp" 
       android:background="@drawable/btn" 
       android:text="Print" /> 

     </RelativeLayout> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 

     <TableLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" > 

      <TableRow 
       android:id="@+id/tableRow7" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" > 

       <WebView 
        android:id="@+id/webViewPrint" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_weight="1" /> 

      </TableRow> 
     </TableLayout> 

    </TableRow> 
</TableLayout> 

這裏是我的活動:

public class VividViewerActivity extends Activity{ 

private UnityPlayer mUnityPlayer; 

private JSInterface mJSInterface; // JavaScript interface (message receiver) 

private WebView mWebView;   // WebView object 
    private ProgressBar mProgress;  // Progress bar 
    private int mLeftMargin;   // Margins around the WebView 
    private int mTopMargin; 
    private int mRightMargin; 
    private int mBottomMargin; 
    private boolean mInitialLoad; 
    Context context; 

    PrintView pv; 

protected void onCreate (Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    mUnityPlayer = new UnityPlayer(this); 

    context = this; 

    if (mUnityPlayer.getSettings().getBoolean ("hide_status_bar", true)) 
     getWindow().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, 
           WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1); 
    boolean trueColor8888 = false; 
    mUnityPlayer.init(glesMode, trueColor8888); 

    View playerView = mUnityPlayer.getView(); 
    setContentView(playerView); 
    playerView.requestFocus(); 

    mJSInterface = new JSInterface();   

    Log.e("Cookie", "COOKIE SUPPORT!"); 

     mWebView = new WebView(this); 

     FrameLayout layout = new FrameLayout(this); 
     addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
     layout.addView(mWebView, new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.NO_GRAVITY)); 
     // Basic settings of WebView. 

     mWebView.setBackgroundColor(Color.TRANSPARENT); 

     WebSettings webSettings = mWebView.getSettings(); 
     webSettings.setSupportZoom(false); 
     webSettings.setJavaScriptEnabled(true); 
     webSettings.setPluginsEnabled(true); 



     mWebView.setWebViewClient(new WebViewClient(){}); 

     pv = new PrintView(context); 
     layout.addView(pv, new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.NO_GRAVITY)); 
     mWebView.addJavascriptInterface(mJSInterface, "UnityInterface"); 

     // Start in invisible state. 
     mWebView.setVisibility(View.GONE); 

     pv.setVisibility(View.INVISIBLE); 

} 

這裏是方法:

public void loadPrintActivity(String printUrl){ 
    Constant.printUrl = printUrl; 
    Constant.isPrintButtonVisible = true; 
    WebSettings webSettings = pv.wev.getSettings(); 
    webSettings.setSupportZoom(true); 
    webSettings.setJavaScriptEnabled(true); 
    webSettings.setPluginsEnabled(true); 
    pv.wev.setWebViewClient(new WebViewClient(){}); 
    pv.wev.loadUrl(printUrl); 
    pv.setVisibility(View.VISIBLE); } 
+0

有沒有人可以幫助我PLZ ............. –

+0

在您的視圖中實現'OnTouchListener'。 – zanky

+0

@ zanky它不工作的兄弟! –

回答

0

更改您的視圖這樣的代碼,

View v=View.inflate(context, R.layout.printscreen, this); 
    wev = (WebView) v.findViewById(R.id.webViewPrint); 
    btnClose = (Button)v.findViewById(R.id.btnClose); 
    btnPrint = (Button)v.findViewById(R.id.btnPrint); 
+0

這不工作的兄弟.... Plz幫助我。 –

0

你必須將此屬性設置爲孩子視圖。

在parenview版式文件

android:clickable="true" 

在Childview佈局文件

android:clickable="false" 

這將是工作。

+0

可以請你告訴我我的XML文件的哪個位置,我必須設置? –

+0

將webview可點擊的真值和智利節點設置爲false。 – Harshid

+0

它還沒有工作........ –