2012-02-17 149 views
2

我想寫我的第一個應用程序 - 一個簡單的應用程序,其中用戶輸入十六進制顏色代碼(EditText),命中輸入(按鈕),並改變ImageView的背景顏色到用戶輸入的十六進制代碼。我如何看待它,我將不得不從edittext中獲取文本,將gettext寫入文件,然後編輯文件以在十六進制代碼之前附加0xAA,以便可以在ImageView.setBackgroundColor(0xAA「HEXHEX」)中輸入它。請讓我知道我該如何做到這一點,或者如果這是做到這一點的正確方法。Android:更改ImageView背景與EditText輸入

這裏是我的Java至今(上按一下按鈕,背景顏色變爲白色,明確其恢復爲黑色)

import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.widget.EditText; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.view.View; 

public class ChkhexActivity extends Activity { 
    private EditText hex; 
    private Button chk; 
    private Button clear; 
    private ImageView view; 
    /** Called when the activity is first created. */ 
    @Override 

    public void onCreate(Bundle savedInstanceState) 
    { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      initControls(); 
    } 

    private void initControls() 
    { 
      hex = (EditText)findViewById(R.id.hex); 
      chk = (Button)findViewById(R.id.chk); 
      clear = (Button)findViewById(R.id.clear); 
      view = (ImageView)findViewById(R.id.view); 
      chk.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ Chk(); }}); 
      clear.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ Clear(); }}); 
    } 

    private void Chk() 
    { 
      view.setBackgroundColor(0xFFffffff); 
    } 

    private void Clear() 
    { 
      hex.setText(""); 
      view.setBackgroundColor(0xFF000000); 
    } 
} 
+0

http://stackoverflow.com/questions/1097742/string-to-hex-value並使用'+'來連接字符串。 – 2012-02-17 05:58:55

回答

1

很好的鍛鍊對於一個初學者。

使用Color.parseColor()。不要忘記首先驗證輸入!

view.setBackgroundColor(Color.parseColor(edt.getText().toString())); 
+0

當我嘗試這個並運行應用程序時,點擊檢查按鈕,我得到的錯誤:不幸的是,你的應用程序已停止。我也遇到了與Sandhya.M的方法相同的錯誤。如果我嘗試調試,則會出現Color.class錯誤,要求將android.jar附加到源代碼。我剛剛更新了4.0和4.0.3的源代碼,並將其指向源代碼(/ platforms/android-14 /),但錯誤不會消失。如果我繼續,我也得到ZygoteInit $ MethodAndArgsCaller源找不到錯誤。 – 2012-02-17 10:32:08

+0

@ killgriff.android,當你嘗試它時,輸入是什麼? – st0le 2012-02-17 12:07:06

+0

我試過了,空白,000000和ffffff。 – 2012-02-17 17:55:02

0
every time u want to change the color of imageview based on text entered in edittext.so u cant fix it like this 
    view.setBackgroundColor(0xFFffffff); 
u have to get the text from edittext.some example like this.. 

public class test extends Activity{ 
private EditText ed; 
private Button btn; 
private ImageView iv; 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test); 

     ed=(EditText)findViewById(R.id.editText1); 
     btn=(Button)findViewById(R.id.button1); 
     iv=(ImageView)findViewById(R.id.imageView1); 
     btn.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       String value=ed.getText().toString(); 
       iv.setBackgroundColor(Color.parseColor(value)); 
      } 
     }); 

} 

} 
the edittext text u entered can be like hex format example like #B0171F,#fafad2,..