剛開始學習Android今天早上,需要幫助。我的應用程序中有幾個按鈕,我希望當用戶單擊某個按鈕時會顯示一個圖像,並返回按鈕以加載main.xml。使用onClick按鈕查看
代碼:
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_marginTop="40dp"
android:layout_width="100dp"
android:layout_height="80dp"
android:id="@+id/b1"
android:text="xyz"
android:background="#ff3375"
android:layout_marginLeft="20dp"
/>
<Button
android:layout_marginTop="40dp"
android:layout_width="100dp"
android:layout_height="80dp"
android:id="@+id/b2"
android:layout_toRightOf="@id/b1"
android:text="abc"
android:background="#ff3375"
android:layout_marginLeft="80dp"
/></RelativeLayout>
而且在Activity.java
package com.sam;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class A2Activity extends Activity {
/** Called when the activity is first created. */
Button a,b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
a= (Button) findViewById(R.id.b1);
b= (Button) findViewById(R.id.b2);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});}}
現在,我需要的onclick方法打開裏面添加圖像或一個XML文件,也是一個返回按鈕返回到main.xml
在你的sam.xml文件中你沒有添加任何imageview,那麼當點擊按鈕時將如何顯示圖像 – user1213202
我在後臺添加了該圖像 –