1

私は頭が回らない問題があります。私はAndroidにかなり慣れていないので、ボタンを使用してレイアウトビューを変更するという基本的な概念に苦労しています。私は自分の活動をメインにしています

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bg"
android:orientation="vertical"
tools:context=".MainActivity" >

<DigitalClock
    android:id="@+id/DigitalClock"
    android:layout_width="fill_parent"
    android:layout_height="100dp"

    android:textColor="#ffffff"
    android:gravity="center"
    android:textSize="70sp" />


<View 
    android:layout_width="fill_parent"
    android:layout_height="3dp"
    android:background="@color/line"
    />

 <Button
 android:id="@+id/AddAlarm"
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:text="@string/add_button"
  />


   <Button
 android:id="@+id/AddAlarm2"
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:text="@string/add_button"
 />

AddAlarm ボタンで add_alarm レイアウトを開くことができます。しかし、私が add_alarm を開いたとき、そのビューには add_alarm2 を開く必要があるボタンがありますが、機能しません これが私の MainActivity.java です:

package com.example.emma;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu; 
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button; 
import android.widget.DigitalClock;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DigitalClock dc = (DigitalClock) findViewById(R.id.DigitalClock);



       final Button button = (Button) findViewById(R.id.AddAlarm);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                setContentView(R.layout.add_alarm);

            }
        });



         final Button button1 = (Button) findViewById(R.id.AddAlarm2);
            button1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    setContentView(R.layout.add_alarm2); 

                }
            });

 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

ボタンクリックを使用してビューを変更する方法を理解していますが、明らかに何か間違っています。これが私のadd_alarm2レイアウトコードです。しかし、imagebutton android:id="@+id/AddAlarm2" をクリックすると、ビュー add_alarm2.xml を開く代わりに何もしません。

ここに私の add_alarm2.xml ファイルがあります:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/bg" 
tools:context=".MainActivity" >

<ImageButton
    android:id="@+id/AddAlarm2"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@null"
    android:src="@drawable/normal"
    android:scaleType="fitCenter"
   />

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@color/line"
    />


</LinearLayout>
4

1 に答える 1

0

手早く汚い方法は、表示したいビューを activity_main.xml 内に配置することですが、 visibility=GONE を指定ます。view.setVisibility(View.VISIBLE)を使用します。

次に、ボタンをクリックすると、この要素の可視性が VISIBLE に切り替わります。setContentView は、onCreate() で 1 回だけ使用する必要があります。複数回呼び出すと、予期しない結果が生じる可能性があります。

より良い方法は、 ViewSwitcherなどの他の代替手段を検討することです。これにより、コードがよりクリーンになります。

于 2013-11-03T22:58:36.730 に答える