Android-使用不同的按钮显示不同的布局

时间:2018-11-01 14:49:14

标签: android

我创建了4个不同的<LinearLayout>和4个不同的<button>。我想为每个按钮显示不同的布局。

我已经尝试过了:

public void changeToLinear1(View view) {
        layoutPrimer.setVisibility(View.VISIBLE);
        layoutSegon.setVisibility(View.GONE);
        layoutPostre.setVisibility(View.GONE);
        layoutExtra.setVisibility(View.GONE);
    }

public void changeToLinear2(View view) {
    layoutPrimer.setVisibility(View.GONE);
    layoutSegon.setVisibility(View.VISIBLE);
    layoutPostre.setVisibility(View.GONE);
    layoutExtra.setVisibility(View.GONE);
}

public void changeToLinear3(View view) {
    layoutPrimer.setVisibility(View.GONE);
    layoutSegon.setVisibility(View.GONE);
    layoutPostre.setVisibility(View.VISIBLE);
    layoutExtra.setVisibility(View.GONE);
}

public void changeToLinear4(View view) {
    layoutPrimer.setVisibility(View.GONE);
    layoutSegon.setVisibility(View.GONE);
    layoutPostre.setVisibility(View.GONE);
    layoutExtra.setVisibility(View.VISIBLE);
}



<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Primers"
        android:id="@+id/btnPrimers"
        android:layout_weight="1"
        android:onClick="changeToLinear1"/>

还有3个具有不同数据的按钮,“ onClick”分别调用各自的功能。

我该如何做到这一点?有更好的方法吗?

4 个答案:

答案 0 :(得分:3)

您可以让所有四个按钮调用相同的方法:

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Primers"
    android:id="@+id/btnPrimers"
    android:layout_weight="1"
    android:onClick="changeToLinear"/>

<Button
    ...
    android:onClick="changeToLinear"/>

<Button
    ...
    android:onClick="changeToLinear"/>

<Button
    ...
    android:onClick="changeToLinear"/>

public void changeToLinear(View view) {
    layoutPrimer.setVisibility(view.getId() == R.id.btnPrimers ? View.VISIBLE : View.GONE);
    layoutSegon.setVisibility(view.getId() == R.id.XXXXXXX ? View.VISIBLE : View.GONE);
    layoutPostre.setVisibility(view.getId() == R.id.YYYYYYY ? View.VISIBLE : View.GONE);
    layoutExtra.setVisibility(view.getId() == R.id.ZZZZZZZ ? View.VISIBLE : View.GONE);
}

(我不知道您其他三个按钮的id,但您可以填写它们。)

view参数是被触发触发click事件的任何视图,因此您可以使用它来确定单击了哪个按钮。

答案 1 :(得分:1)

public void changeToLinear(int btnId) {
    layoutPrimer.setVisibility(btnId == R.id.btn1 ? View.VISIBLE : View.GONE);
    layoutSegon.setVisibility(btnId == R.id.btn2 ? View.VISIBLE : View.GONE);
    layoutPostre.setVisibility(btnId == R.id.btn3 ? View.VISIBLE : View.GONE);
    layoutExtra.setVisibility(btnId == R.id.btn4 ? View.VISIBLE : View.GONE);
}

现在,您已经实现了onClick()方法:

public void onClick(View v) {
    switch(v.getId()) {
        case R.id.btn1:
        case R.id.btn2:
        case R.id.btn3:
        case R.id.btn4:
            changeToLinear(v.getId());
            break;
    }
}

答案 2 :(得分:1)

我要回答,因为我就像@BenP hit send一样写了这个。基本上是相同的建议,但是要在活动onCreate中添加点击侦听器并调用类似的函数。

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

    findViewById(R.id.btnPrimers).setOnClickListener(view -> updateLayouts(1));
    findViewById(R.id.btnSegon).setOnClickListener(view -> updateLayouts(2));
    findViewById(R.id.btnPostre).setOnClickListener(view -> updateLayouts(3));
    findViewById(R.id.btnExtra).setOnClickListener(view -> updateLayouts(4));
}

private void updateLayouts(int layout) {
    layoutPrimer.setVisibility(layout == 0 ? View.VISIBLE : View.GONE);
    layoutSegon.setVisibility(layout == 1 ? View.VISIBLE : View.GONE);
    layoutPostre.setVisibility(layout == 2 ? View.VISIBLE : View.GONE);
    layoutExtra.setVisibility(layout == 3 ? View.VISIBLE : View.GONE);
}

答案 3 :(得分:1)

片段是实现此类功能的最佳方法。

下面是实现片段的代码,该片段将根据不同的按钮单击而更改。

主要活动布局:

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="vertical" >
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Fragment One"/>
        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Fragment Two"/>
        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.concretepage.android.FragmentOne"
            android:id="@+id/output"/>
    </LinearLayout> 

分割一种布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:id="@+id/msg1"/>
</LinearLayout>  

分段两个布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:gravity="center"
    android:background="#b0b0ff"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="#FF0000"
        android:textSize="20sp"
        android:id="@+id/msg2"/>
</LinearLayout> 

FragmentOne.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.cp.android.R;
public class FragmentOne extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one, viewGroup, false);
        TextView output= (TextView)view.findViewById(R.id.msg1);
        output.setText("Fragment One");
        return view;
    }
}  

FragmentTwo.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.cp.android.R;
public class FragmentTwo extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_two, viewGroup, false);
        TextView output= (TextView)view.findViewById(R.id.msg2);
        output.setText("Fragment Two");
        return view;
    }
}  

MainActivity.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.cp.android.R;
public class MainActivity extends FragmentActivity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        OnClickListener listener = new OnClickListener() {
        public void onClick(View view) {
            Fragment fragment = null;
            if(view == findViewById(R.id.button1)){
                      fragment = new FragmentOne();
            } else {
                      fragment = new FragmentTwo();                 
            }
                        FragmentManager manager = getSupportFragmentManager();
                        FragmentTransaction transaction = manager.beginTransaction();
                        transaction.replace(R.id.output, fragment);
                        transaction.commit();
        }
         };
        Button btn1 = (Button)findViewById(R.id.button1);
        btn1.setOnClickListener(listener);
        Button btn2 = (Button)findViewById(R.id.button2);
        btn2.setOnClickListener(listener);
    }
}