如何在不同的活动中调用/调用重复按钮的类

时间:2014-09-14 11:23:09

标签: java android class button android-activity

我的应用程序中有大约20个活动,每个活动都有相同的按钮,因此我尝试为每个按钮设置一个类,以避免在每个活动中重复相同的代码。我怎么不知道如何做到这一点或如何在活动中调用它们:

这里是每个活动代码中重复按钮内容的示例: (i5,i6,i7 ......是按钮变量)

    i5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    p++;
    switch (p) {
    case 1:
        Typeface tf3 = Typeface.createFromAsset(getAssets(),
                "fonts/dubai.TTF");
        t2.setTypeface(tf3);
        break;
    case 2:
        Typeface tf4 = Typeface.createFromAsset(getAssets(),
                "fonts/kacstpen.ttf");
        t2.setTypeface(tf4);
        break;
    case 3:
        Typeface tf5 = Typeface.createFromAsset(getAssets(),
                "fonts/mohammad.ttf");
        t2.setTypeface(tf5);
        break;
    case 4:
        Typeface tf6 = Typeface.createFromAsset(getAssets(),
                "fonts/nassim.ttf");
        t2.setTypeface(tf6);
        p = 0;
        break;
    }
}
});
i10.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    Intent i = new Intent(getApplicationContext(), Content12.class);
    startActivity(i);
    overridePendingTransition(R.anim.pushin, R.anim.pushout);
}
});
i8.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    Intent i = new Intent(getApplicationContext(), Content10.class);
    startActivity(i);
    overridePendingTransition(R.anim.pushinright,
            R.anim.pushoutright);
}
});
i1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    Intent i = new Intent(getApplicationContext(), Info.class);
    startActivity(i);
    overridePendingTransition(R.anim.pushinhorizontal,
            R.anim.pushouthorizontal);
}
});
i12.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    Intent i = new Intent (getApplicationContext(), Apps.class);
    startActivity(i);
    overridePendingTransition(R.anim.pushin, R.anim.pushout);
}
});
i3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    o++;
    switch (o) {
    case 1:
        layout2.setBackgroundResource(R.drawable.bac10);
        break;
    case 2:
        layout2.setBackgroundColor(Color.parseColor("#95B9C7"));
        break;
    case 3:
        layout2.setBackgroundColor(Color.parseColor("#82CAFA"));
        break;
    case 4:
        layout2.setBackgroundColor(Color.parseColor("#CCFFFF"));
        break;
    case 5:
        layout2.setBackgroundColor(Color.parseColor("#6CC417"));
        break;
    case 6:
        layout2.setBackgroundColor(Color.parseColor("#8AFB17"));
        break;
    case 7:
        layout2.setBackgroundColor(Color.parseColor("#FFA500"));
        break;
    case 8:
        layout2.setBackgroundColor(Color.parseColor("#ADD8E6"));
        break;
    case 9:
        layout2.setBackgroundColor(Color.parseColor("#E6A9EC"));
        break;
    case 10:
        layout2.setBackgroundColor(Color.parseColor("#87F717"));
        break;
    case 11:
        layout2.setBackgroundColor(Color.parseColor("#F9966B"));
        break;
    case 12:
        layout2.setBackgroundResource(R.drawable.bac6);
        break;
    default:
        layout2.setBackgroundResource(R.drawable.backbleu);
        o = 0;
    }
}
});
i4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    r++;
    switch (r) {
    case 1:
        t2.setTextColor(Color.parseColor("#151B54"));
        break;
    case 2:
        t2.setTextColor(Color.parseColor("#254117"));
        break;
    case 3:
        t2.setTextColor(Color.parseColor("#AF7817"));
        break;
    case 4:
        t2.setTextColor(Color.MAGENTA);
        break;
    case 5:
        t2.setTextColor(Color.parseColor("#0000A0"));
        break;
    case 6:
        t2.setTextColor(Color.parseColor("#C0C0C0"));
        break;
    case 7:
        t2.setTextColor(Color.parseColor("#8C001A"));
        break;
    case 8:
        t2.setTextColor(Color.parseColor("#437C17"));
        break;
    case 9:
        t2.setTextColor(Color.parseColor("#4B0082"));
        break;
    case 10:
        t2.setTextColor(Color.parseColor("#800517"));
        break;
    case 11:
        t2.setTextColor(Color.RED);
        break;
    case 12:
        t2.setTextColor(Color.GREEN);
        break;
    case 13:
        t2.setTextColor(Color.parseColor("#C11B17"));
        break;
    default:
        t2.setTextColor(Color.BLACK);
        r = 0;
    }
}
});
i6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    if (i < 50)
        i = i + 2;
    t2.setTextSize(i);
}
});
 i7.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    if (i > 16)
        i = i - 2;
    t2.setTextSize(i);
}
});
i9.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    finish();
    overridePendingTransition(R.anim.pushinright,
            R.anim.pushoutright);
}
});

1 个答案:

答案 0 :(得分:0)

我建议你使用inheritance。创建一个{20}不同super class扩展的Activities

然后在您的super class中,您应该创建所有观看次数,并将OnClickListeners添加到Buttons

示例可能如下所示:

<强> activity_base.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".BaseActivity">

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My button" />

</RelativeLayout>

超级(基础活动):

public abstract class BaseActivity extends Activity {
    private Button myButton;
    private int value;

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

    public void initialiseViews(final Activity activity) {
        myButton = (Button) findViewById(R.id.myButton);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(BaseActivity.this, "myButton clicked from " + activity.getClass().getName(), Toast.LENGTH_LONG).show();
            }
        });
    }

    public void setValue(int newValue) {
        this.value = newValue;
    }

    public int getValue() {
        return value;
    }
}

从超类继承的活动:

public class MyActivity extends BaseActivity {

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

        setValue(789); // Setting value inherited from our base-class.
    }
}

你可以在这段代码中添加很多东西(例如回调),但这只是一个基本的例子,只是为了让你的开始。

旁注:在“冒险”进入Android之前,您应该花一些时间了解Java的继承和基本概念 - 如果有的话,您可能已经能够回答你自己的问题; - )