2015-04-13 73 views
1

我有在activity_main.xml中按鈕的問題,的Android的ImageButton的onClick

  1. 我需要在應用程序上的 「imageButton8」 網頁視圖(網站URL)的點擊來啓動。

MainActivity.Java

package com.XX.app; 

import com.XX.app.R; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.WindowManager; 
import android.webkit.WebChromeClient; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Button; 
import android.widget.ImageButton; 

public class MainActivity extends Activity { 
    WebView view; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //Empêcher le téléphone de passer en mode veille 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

     //When User click on the button imageButton8 the Activity2 launch 
     Button imageButton8 = (Button) findViewById(R.id.imageButton8); 
       setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       Intent intent = new Intent(v.getContext(), Activity2.class); 
       startActivityForResult(intent, 0); 
       } 
      }); 
      } 

    private void setOnClickListener(OnClickListener onClickListener) { 
     // TODO Auto-generated method stub 

    } 




    //loads RETURN URL on lastpage 
@Override 
public void onBackPressed() { 

    if(this.view.canGoBack()) 
    { 
     this.view.goBack(); 

    } else { 
     new AlertDialog.Builder(this) 
      .setIcon(android.R.drawable.ic_dialog_alert) 
      .setTitle("إغلاق تطبيق جمعية البر و التعاون") 
      .setMessage("هل أنت متأكد أنك تريد إغلاق تطبيق جمعية البر و التعاون ؟") 
      .setPositiveButton("نعم", new DialogInterface.OnClickListener() 
      { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       finish();  
      } 

     }) 
     .setNegativeButton("لا", null) 
     .show(); 
    } 

    } 

} 

activity_main.xml中爲imageButton8是

<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" 
android:background="@drawable/background_1"> 

<ImageButton 
    android:id="@+id/imageButton8" 
    android:onClick="click" 
    android:layout_width="90dp" 
    android:layout_height="90dp" 
    android:layout_marginLeft="140dp" 
    android:layout_marginTop="380dp" 
    android:background="#0000" 
    android:scaleType="fitXY" 
    android:src="@drawable/btn_www" /> 
</RelativeLayout> 

Activity2.java

package com.XX.app; 

import com.XX.app.R; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.app.Application; 
import android.os.Bundle; 

public class Activity2 extends Activity { 
    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_webview); 
    } 
} 

activity_webview.xml

<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" 
    android:background="@drawable/background_1"> 

    <WebView 
     android:id="@+id/webView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" /> 
</RelativeLayout> 

在同一個項目中, 2.其他問題:我有08鍵。我如何執行其模板xml中的每個按鈕?

例如:按鈕1 => about_obama.xml。當用戶點擊(在這個文件中)它會找到奧巴馬的描述。 Button 2 => .... xml ... etc! 按鈕3 ......

回答

2

您需要使用ImageButton的setOnClickListener()方法爲ImageButton設置onclickListener。所以,在你MainActivity,您的onCreate()方法中,執行以下操作:

Button imageButton8 = (Button) findViewById(R.id.imageButton8); 
// Set the OnClickListener on the button itself 
imageButton8.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     Intent intent = new Intent(MainActivity.this, Activity2.class); // Use the MainActivity context 
     startActivityForResult(intent, 0); 
    } 
}); 
// You should delete the private setOnClickListener() method as it does not 
// have any purpose 

要指定的onClick()在XML中單擊該按鈕時,設置:

android:onClick:"some_method" 

財產爲您的按鈕。然後在你的Activity中聲明和定義這個方法。