我在activity_main.xml中的按钮有问题,
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();
}
}
}
imageButton8的activity_main.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">
<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 =&gt; about_obama.xml。当用户点击(在此文件中)时,它将找到obama的描述。 按钮2 =&gt; .... XML ...等等! 按钮3 ......
答案 0 :(得分: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
要在单击Button时指定xml中的onClick(),请设置:
android:onClick:"some_method"
按钮的属性。然后在Activity中声明并定义此方法。