【问题标题】:changing the color of surfaceview using buttons in android使用android中的按钮更改surfaceview的颜色
【发布时间】:2016-03-05 17:47:59
【问题描述】:

我正在尝试使用按钮更改表面视图的颜色或背景以更改不同的颜色。我上网并看到很多告诉我在 XML 中使用 onClick="changeblue" 并创建一个简单的方法 "changeblue" 将更改表面视图的颜色但由于某种原因它无法找到表面视图背景颜色或只是表面视图颜色。有任何想法吗?

XML:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Color Change App"
    android:id="@+id/txt"
    android:textSize="50dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="98dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Changes to blue"
    android:id="@+id/btn1"
    android:layout_marginTop="164dp"
    android:layout_below="@+id/txt"
    android:layout_alignParentStart="true"
    android:background="#aeb3b5"
    android:clickable="false"
    android:textColor="#1b28b8"
    android:onClick="btn1"
    android:enabled="false" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change to green"
    android:id="@+id/btn2"
    android:layout_alignBottom="@+id/btn1"
    android:layout_centerHorizontal="true"
    android:background="#aeb3b5"
    android:clickable="false"
    android:textColor="#098c07"
    android:onClick="btn2"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Changes to white"
    android:id="@+id/btn3"
    android:layout_alignBottom="@+id/btn2"
    android:layout_alignParentEnd="true"
    android:background="#aeb3b5"
    android:clickable="false"
    android:textColor="#ffffff"
    android:onClick="btn3"/>

<SurfaceView
    android:layout_width="500dp"
    android:layout_height="400dp"
    android:id="@+id/surfaceview"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true" />
</RelativeLayout>

Java:

package com.example.nathan.colorchangeapp;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.graphics.Color;
import android.view.View;


public class MainActivity extends ActionBarActivity {

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

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void btn1(View view) {
    //change color of surfaceview to blue
    SurfaceView.//no background or color option
}
public void btn2(View view) {
    //change color of surfaceview to green
}
public void btn3(View view) {
    //change color of srufaceview to white
}


}

【问题讨论】:

    标签: android android-studio android-button


    【解决方案1】:

    我能够弄清楚。 btn1、btn2、btn3 和surfaceView 是XML 页面上的id。我发现也没有在 XML 上使用 onclick。

    package com.example.nathan.colorchange;
    
    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.SurfaceView;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends ActionBarActivity {
    Button bluebtn, greenbtn, whitebtn;
    SurfaceView SView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        bluebtn=(Button)findViewById(R.id.btn1);
        greenbtn=(Button)findViewById(R.id.btn2);
        whitebtn=(Button)findViewById(R.id.btn3);
        SView=(SurfaceView)findViewById(R.id.surfaceView);
    
    
        bluebtn.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
            SView.setBackgroundColor(Color.BLUE);
            }
        });
        greenbtn.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                SView.setBackgroundColor(Color.GREEN);
            }
        });
        whitebtn.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                SView.setBackgroundColor(Color.WHITE);
            }
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      相关资源
      最近更新 更多