簡體   English   中英

如何使用按鈕更改背景顏色?

[英]How to change the background color with a button?

如何在Android Studio中通過按一下按鈕隨機更改背景顏色?

這是我的代碼:

public class partymodus extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_partymodus);

    final TextView aufgabe=(TextView)findViewById(R.id.txt_aufgabe);
    final Button next =  (Button)findViewById(R.id.btn_next);

    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            String[] aufgaben = getResources().getStringArray(R.array.name);
            Random rand = new Random();
            int n = rand.nextInt(aufgaben.length - 0) + 0;
            aufgabe.setText(aufgaben[n]);

        }
    });
}

您必須在按鈕上添加一個ActionListener並重寫該方法:

public void actionPerformed(ActionEvent e) { 
    ...//code that reacts to the action... 
}

對不起,我的英語我是新手

作為非專業人士來回答您的問題,您必須在正式API中先弄清楚jave圖形設計模式是什么,這是搜索JFrameJava.awt.Graphic的鏈接, [1]

由於不是使用按鈕組件構建的,因此不是從此處開始在圖形上構建組件的位置,我建議您查看JFrame的Inherit或父接口,以及另一個Containers。

MyActivity.setOnClickListener(new View.OnClickListener()       {

@Override
public void onClick(View v) {

MyActivity.setBackgroundColor(getResources().getColor(R.color.RED));
}

添加view_name.setBackgroundColor(getResources().getColor(R.color.colorAccent)); 設置背景色的線

這是我的代碼:

   mButton1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mHomeLl.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM