2012-02-17 130 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,..