TypeCasting在Android中

时间:2012-05-13 20:54:55

标签: android casting

无法在

中使用类型转换
static String fontPath, temp, textColor;
static EditText text;
static TextView tv;
static Typeface tf;

案例1:

tv.setTextColor(Color.parseColor(textColor); // NOT WORKING

所以

使用无限的If else语句..比如..

if (textColor=="#FF0000") 
    tv.setTextColor(Color.parseColor("#FF0000")); // ROUGH SOLUTION

案例2:

同样在

text = (EditText) findViewById(R.id.et1);
temp = text.getText().toString();

text.setText(temp); // NOT WORKING -- BLANK TEXT

如果其他陈述不可能......

Toast.makeText(Opt.this, temp, Toast.LENGTH_SHORT).show();  

这完美无缺......解决方案..?

编辑...... 1:

亲爱的 dmon

我想传递变量而不是固定值......

setText();
setTextColor();
setTypeface();

到TextView对象

&安培;一个

setText();

到EditText对象......

尝试上面的代码无效..?

完整代码......

package cts.t.tab;

import java.util.ArrayList;
import java.util.List;


import cts.t.tab.R;

import android.R.string;
import android.app.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;

import android.graphics.*;

import android.os.Bundle;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.KeyEvent;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class Text extends Activity { 

static String fontPath, item, temp, textColor;
static TextView tv;
static Typeface tf;
static int size=50;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.text);

    click();
    listen();   
    size();
    display();
    //draw();
} 


public void click() {
    // TODO Auto-generated method stub
    final Button bo = (Button) findViewById(R.id.bo); 
    bo.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

          Intent i = new Intent();
          i.setClassName("cts.t.tab", "cts.t.tab.Opt"); // 
          startActivity(i);
        }
    });
    final Button be = (Button) findViewById(R.id.be); 
    be.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

          //

        }
    });
}


public void draw() {

    setContentView(new SampleView(this));

}

private static class SampleView extends View {
    private Paint    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Typeface mFace;

    public SampleView(Context context) {                    
        super(context);

        mFace = Typeface.createFromAsset(getContext().getAssets(),fontPath);
        mPaint.setTextSize(size);

    }

    @Override 
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        mPaint.setTypeface(null);                   
        canvas.drawText("Default", 10, 100, mPaint);
        mPaint.setTypeface(mFace);
        canvas.drawText(temp, 10, 200, mPaint);
    }
}


public void size() {

    final TextView tv = (TextView) findViewById(R.id.tv);       
    final SeekBar sb = (SeekBar)findViewById(R.id.sb);

    sb.setMax(100);
    sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            if(progress==0)size=1;
            else size=progress;

            display();
        }
    });
}


public void display() {

    TextView tv = (TextView)findViewById(R.id.tv); 
    //Toast.makeText(Text.this, "font @ Display : " + fontPath +" ", Toast.LENGTH_SHORT).show();
    //Typeface tf = Typeface.createFromAsset( getAssets(), fontPath);

    // TODO: Why if(fontPath.equals("EN_1.ttf")) is not working ?
    // TODO: Why "  Typeface tf = Typeface.createFromAsset( getAssets(), fontPath); " is not working ..?
    // TODO: What is Data Type of fontPath..? What should be ..?


    if(fontPath=="EN_1.ttf") tf = Typeface.createFromAsset( getAssets(), "EN_1.ttf");
    else if(fontPath=="EN_2.ttf") tf = Typeface.createFromAsset( getAssets(), "EN_2.ttf");
    else if(fontPath=="EN_3.ttf") tf = Typeface.createFromAsset( getAssets(), "EN_3.ttf");
    else if(fontPath=="CN_1.ttf") tf = Typeface.createFromAsset( getAssets(), "CN_1.ttf");
    else if(fontPath=="CN_2.ttf") tf = Typeface.createFromAsset( getAssets(), "CN_2.ttf");
    else if(fontPath=="CN_3.ttf") tf = Typeface.createFromAsset( getAssets(), "CN_3.ttf");
    else if(fontPath=="JP_1.ttf") tf = Typeface.createFromAsset( getAssets(), "JP_1.ttf");
    else if(fontPath=="JP_2.ttf") tf = Typeface.createFromAsset( getAssets(), "JP_2.ttf");
    else if(fontPath=="JP_3.ttf") tf = Typeface.createFromAsset( getAssets(), "JP_3.ttf");

    // TODO Same problem of fontPath ...?

    // tv.setTextColor(Color.parseColor(textColor));
    // tv.setTextColor(Color.parseColor(To_c.textColor);
    // tv.setTextColor(Color.parseColor(To_c.getColor2()));

    // TODO Why any one is not working ... ?



//OLD       

    if (textColor=="#FF0000") tv.setTextColor(Color.parseColor("#FF0000"));
    else if (textColor=="#008000") tv.setTextColor(Color.parseColor("#008000"));
    else if (textColor=="#0000FF") tv.setTextColor(Color.parseColor("#0000FF"));
    else if (textColor=="#FFA500") tv.setTextColor(Color.parseColor("#FFA500"));
    else if (textColor=="#00FF00") tv.setTextColor(Color.parseColor("#00FF00"));
    else if (textColor=="#000080") tv.setTextColor(Color.parseColor("#000080"));
    else if (textColor=="#800000") tv.setTextColor(Color.parseColor("#800000"));

//NEW

    else if (textColor=="#942998") tv.setTextColor(Color.parseColor("#942998"));
    else if (textColor=="#0096FF") tv.setTextColor(Color.parseColor("#0096FF"));
    else if (textColor=="#B9DF7D") tv.setTextColor(Color.parseColor("#B9DF7D"));
    else if (textColor=="#DDA0DD") tv.setTextColor(Color.parseColor("#DDA0DD"));
    else if (textColor=="#FF00E4") tv.setTextColor(Color.parseColor("#FF00E4"));
    else if (textColor=="#FDFF14") tv.setTextColor(Color.parseColor("#FDFF14"));
    else if (textColor=="#FA9403") tv.setTextColor(Color.parseColor("#FA9403"));
    else if (textColor=="#FF0000") tv.setTextColor(Color.parseColor("#FF0000"));
    else if (textColor=="#FFFFFF") tv.setTextColor(Color.parseColor("#FFFFFF"));
    else if (textColor=="#000000") tv.setTextColor(Color.parseColor("#000000"));

    tv.setTextSize(size);
    tv.setTypeface(tf) ;
    tv.setText(temp) ;
}



public void listen() {

    fontPath = Opt.getFontPath();
    temp = Opt.getText2();
    textColor = Opt.getColor2();

    if(fontPath != null) 
    Toast.makeText(Text.this, "font : " + fontPath + " ", Toast.LENGTH_SHORT).show();

    if(temp != null)
    Toast.makeText(Text.this, "text : " + temp + " ", Toast.LENGTH_SHORT).show();

    if(textColor != null)
    Toast.makeText(Text.this, "color : " + textColor + " ", Toast.LENGTH_SHORT).show();

}
}

第二类......

package cts.t.tab;

import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class Opt extends Activity { 

static String fontPath, temp, textColor;
static int k=0;
static EditText text;
static Typeface tf1, tf2, tf3;


//  Called when the activity is first created. 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.opt); 

    cancel();
    font();
    color();
    text();


}



     // COLOR BTNs
public void text() {


    // get edittext component
    text = (EditText) findViewById(R.id.et1);
    temp = text.getText().toString();

    text.setText(temp);

    // add akeylistener to keep track user input
    text.setOnKeyListener( new OnKeyListener()  {   

    public boolean onKey(View v, int keyCode, KeyEvent event) {

        // if keydown and "enter" is pressed
        if ( (event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER) ) {
            temp = text.getText().toString();  
            Toast.makeText(Opt.this, temp, Toast.LENGTH_SHORT).show();

            Intent i = new Intent();
            i.setClassName("cts.t.tab", "cts.t.tab.TabMain"); // 
            startActivity(i);

            return true;
            }  
        return false;
    }
    private ColorStateList parseColor(String string) {
        // TODO Auto-generated method stub
        return null;
        }
    });
}

public void color() {

    // TODO Auto-generated method stub
     final Button c1 = (Button) findViewById(R.id.bc1);
     final Button c2 = (Button) findViewById(R.id.bc2);
     final Button c3 = (Button) findViewById(R.id.bc3);
     final Button c4 = (Button) findViewById(R.id.bc4);
     final Button c5 = (Button) findViewById(R.id.bc5);
     final Button c6 = (Button) findViewById(R.id.bc6);
     final Button c7 = (Button) findViewById(R.id.bc7);
     final Button c8 = (Button) findViewById(R.id.bc8);
     final Button c9 = (Button) findViewById(R.id.bc9);
     final Button c10 = (Button) findViewById(R.id.bc10);

     c1.setBackgroundColor(Color.TRANSPARENT);
     c2.setBackgroundColor(Color.TRANSPARENT);
     c3.setBackgroundColor(Color.TRANSPARENT);
     c4.setBackgroundColor(Color.TRANSPARENT);
     c5.setBackgroundColor(Color.TRANSPARENT);
     c6.setBackgroundColor(Color.TRANSPARENT);
     c7.setBackgroundColor(Color.TRANSPARENT);
     c8.setBackgroundColor(Color.TRANSPARENT);
     c9.setBackgroundColor(Color.TRANSPARENT);
     c10.setBackgroundColor(Color.TRANSPARENT);

     c1.setTextColor(Color.TRANSPARENT);
     c2.setTextColor(Color.TRANSPARENT);
     c3.setTextColor(Color.TRANSPARENT);
     c4.setTextColor(Color.TRANSPARENT);
     c5.setTextColor(Color.TRANSPARENT);
     c6.setTextColor(Color.TRANSPARENT);
     c7.setTextColor(Color.TRANSPARENT);
     c8.setTextColor(Color.TRANSPARENT);
     c9.setTextColor(Color.TRANSPARENT);
     c10.setTextColor(Color.TRANSPARENT);

// COLOR BUTTONS

     c1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(Opt.this, "Color : BLACK", Toast.LENGTH_SHORT).show();
            textColor="#000000";
        }
     });

     c2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(Opt.this, "Color : WHITE", Toast.LENGTH_SHORT).show();
            textColor="#FFFFFF";
        }
     });


     c3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(Opt.this, "Color : RED", Toast.LENGTH_SHORT).show();
            textColor="#FF0000";
        }
     });

     c4.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(Opt.this, "Color : ORANGE", Toast.LENGTH_SHORT).show();
            textColor="#FA9403";
        }
     });

     c5.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(Opt.this, "Color : YELLOW", Toast.LENGTH_SHORT).show();
            textColor="#FDFF14";
        }
     });

     c6.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(Opt.this, "Color : PURPLE", Toast.LENGTH_SHORT).show();
            textColor="#FF00E4";
        }
     });

     c7.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(Opt.this, "Color : PINK", Toast.LENGTH_SHORT).show();
            textColor="#DDA0DD";
        }
     });

     c8.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(Opt.this, "Color : GREEN", Toast.LENGTH_SHORT).show();
            textColor="#B9DF7D";
        }
     });

     c9.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(Opt.this, "Color : BLUE", Toast.LENGTH_SHORT).show();
            textColor="#0096FF";
        }
     });

     c10.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Toast.makeText(Opt.this, "Color : VOILET", Toast.LENGTH_SHORT).show();
            textColor="#942998";
        }
     });


}

public void font() {
    // TODO Auto-generated method stub

// DEFINATION

    // FONT BTNs
    final Button ben1 = (Button) findViewById(R.id.ben1);
    final Button ben2 = (Button) findViewById(R.id.ben2);
    final Button ben3 = (Button) findViewById(R.id.ben3);
    final Button bcn1 = (Button) findViewById(R.id.bcn1);
    final Button bcn2 = (Button) findViewById(R.id.bcn2);
    final Button bcn3 = (Button) findViewById(R.id.bcn3);
    final Button bjp1 = (Button) findViewById(R.id.bjp1);
    final Button bjp2 = (Button) findViewById(R.id.bjp2);
    final Button bjp3 = (Button) findViewById(R.id.bjp3);

    final TextView te1 = (TextView) findViewById(R.id.te1);
    final TextView te2 = (TextView) findViewById(R.id.te2);
    final TextView te3 = (TextView) findViewById(R.id.te3);
    final TextView tc1 = (TextView) findViewById(R.id.tc1);
    final TextView tc2 = (TextView) findViewById(R.id.tc2);
    final TextView tc3 = (TextView) findViewById(R.id.tc3);
    final TextView tj1 = (TextView) findViewById(R.id.tj1);
    final TextView tj2 = (TextView) findViewById(R.id.tj2);
    final TextView tj3 = (TextView) findViewById(R.id.tj3);


    // FONT BTNs
    ben1.setBackgroundColor(Color.TRANSPARENT);
    ben2.setBackgroundColor(Color.TRANSPARENT);
    ben3.setBackgroundColor(Color.TRANSPARENT);
    bcn1.setBackgroundColor(Color.TRANSPARENT);
    bcn2.setBackgroundColor(Color.TRANSPARENT);
    bcn3.setBackgroundColor(Color.TRANSPARENT);
    bjp1.setBackgroundColor(Color.TRANSPARENT);
    bjp2.setBackgroundColor(Color.TRANSPARENT);
    bjp3.setBackgroundColor(Color.TRANSPARENT);
    //cbc.setBackgroundColor(Color.TRANSPARENT);


    te1.setTextColor(Color.TRANSPARENT);
    te2.setTextColor(Color.TRANSPARENT);
    te3.setTextColor(Color.TRANSPARENT);
    tc1.setTextColor(Color.TRANSPARENT);
    tc2.setTextColor(Color.TRANSPARENT);         
    tc3.setTextColor(Color.TRANSPARENT);
    tj1.setTextColor(Color.TRANSPARENT);
    tj2.setTextColor(Color.TRANSPARENT);
    tj3.setTextColor(Color.TRANSPARENT);

    tf1 = Typeface.createFromAsset( getAssets(), "EN_1.ttf");
    ben1.setTypeface(tf1);

    tf2 = Typeface.createFromAsset( getAssets(), "EN_2.ttf");
    ben2.setTypeface(tf2);

    tf3 = Typeface.createFromAsset( getAssets(), "EN_3.ttf");
    ben3.setTypeface(tf3);

    ben1.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             fontPath = "EN_1.ttf"; 
             Toast.makeText(Opt.this, "Font : " + fontPath, Toast.LENGTH_SHORT).show();
         }
     });
     ben2.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             fontPath = "EN_2.ttf";
             Toast.makeText(Opt.this, "Font : " + fontPath, Toast.LENGTH_SHORT).show();
         }
     });
     ben3.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             fontPath = "EN_3.ttf";
             Toast.makeText(Opt.this, "Font : " + fontPath, Toast.LENGTH_SHORT).show();
         }
     });
     bcn1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            fontPath = "CN_1.ttf";
            Toast.makeText(Opt.this, "Font : " + fontPath, Toast.LENGTH_SHORT).show();
        }
     });
     bcn2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            fontPath = "CN_2.ttf";
            Toast.makeText(Opt.this, "Font : " + fontPath, Toast.LENGTH_SHORT).show();
        }
     });
     bcn3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            fontPath = "CN_3.ttf";
            Toast.makeText(Opt.this, "Font : " + fontPath, Toast.LENGTH_SHORT).show();
        }
     });
     bjp1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            fontPath = "JP_1.ttf";
            Toast.makeText(Opt.this, "Font : " + fontPath, Toast.LENGTH_SHORT).show();
        }
     });
     bjp2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            fontPath = "JP_2.ttf";
            Toast.makeText(Opt.this, "Font : " + fontPath, Toast.LENGTH_SHORT).show();
        }
     });
     bjp3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            fontPath = "JP_3.ttf";
            Toast.makeText(Opt.this, "Font : " + fontPath, Toast.LENGTH_SHORT).show();
        }
     });
}

public void cancel() {
    // TODO Auto-generated method stub

    final Button bc = (Button) findViewById(R.id.bc); 
    bc.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

          Intent i = new Intent();
          i.setClassName("cts.t.tab", "cts.t.tab.TabMain"); // 
          startActivity(i);
        }
    });
}

public static String getFontPath()
{
    return fontPath;
}

public static int getCounter()
{
    return k;
}

public static String getText2()
{
    return temp;
}

public static String getColor2()
{
    return textColor;
}
}

1 个答案:

答案 0 :(得分:2)

你应该清理你的问题,根本不清楚你在问什么。我可以告诉你的是tv.setTextColor()没有Color对象(docs),它需要一个整数。你想要的是以下几点:

    tv.setTextColor(0xFFFF0000);

或者,更好的是,只需将您想要的颜色放在resource file中并使用它

    tv.setTextColor(getResources().getColor(R.color.red));