Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除Android布局和活动代码中的重复_Java_Android_Android Layout_Android Activity - Fatal编程技术网

Java 删除Android布局和活动代码中的重复

Java 删除Android布局和活动代码中的重复,java,android,android-layout,android-activity,Java,Android,Android Layout,Android Activity,我有一个简单的Activity/LinearLayout组合,它有两个按钮,单击每个按钮时会更改背景颜色。代码如下 在我看来,有很多高级代码重复。例如,我不需要在Java文件中使用按钮和LinearLayout后台定义和生成变量。当我表达我想要把它们结合在一起的意图时,我应该会自动得到它们 因此,像btnBlue=(Button)findViewById(R.id.btnBlue)这样的行应该不是必需的。一个好的框架应该允许我使用一些约定(比如如果我的资源文件@+id/resource被命名为b

我有一个简单的Activity/LinearLayout组合,它有两个按钮,单击每个按钮时会更改背景颜色。代码如下

在我看来,有很多高级代码重复。例如,我不需要在Java文件中使用按钮和LinearLayout后台定义和生成变量。当我表达我想要把它们结合在一起的意图时,我应该会自动得到它们

因此,像
btnBlue=(Button)findViewById(R.id.btnBlue)这样的行应该不是必需的。一个好的框架应该允许我使用一些约定(比如如果我的资源文件@+id/resource被命名为btnBlue,那么给我一个Java变量btnBlue,等等)

我在哪里可以找到一个在Android编程中为我实现这些功能的框架

谢谢

布局xml

<?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:layout_height="match_parent"
    android:background="#ff000000"

    android:weightSum="1"
    android:id="@+id/background">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Linear Layout Tutorial"
        android:textColor="#ff33ff"
        android:textSize="32sp"
        />

    <Button
        android:id="@+id/btnGreen"
        android:layout_width="177dp"
        android:layout_height="wrap_content"
        android:text="Change to Green"


        android:layout_weight="0.07" />

    <Button
        android:id="@+id/btnBlue"
        android:layout_width="400sp"
        android:layout_height="wrap_content"
        android:text="Change12 to Blue"

        android:layout_weight="0.19" />


</LinearLayout>

Jake Wharton为您的问题提供了一个很好的解决方案。

谢谢。在Android中,哪一个更适合注入和减少代码-AQuery还是Butterknife?如果您只想查看注入并避免您在问题中提出的样板代码,我会选择Butterknife。一个很大的区别是AQuery似乎使用反射进行绑定,而st Butterknife使用注释和预处理。因此Butterknife在这方面更快。
package com.example.xxx.testandroid;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.renderscript.Sampler;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class MainActivity extends Activity {

    private final String TAG = "TKT";
    LinearLayout background;
    Button btnGreen, btnBlue;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.som_linear);
        Log.d(TAG, "onCreate");

        background = (LinearLayout) findViewById(R.id.background);
        btnBlue = (Button) findViewById(R.id.btnBlue);
        btnGreen = (Button) findViewById(R.id.btnGreen);

        btnGreen.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // click button code here
                background.setBackgroundColor(Color.parseColor("#00ff00"));
            }
        });

        btnBlue.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                // click button code here
                background.setBackgroundColor(Color.parseColor("#006699"));
            }
        });

    }



}