如何获取textview和settextcolor的id到所有textview

时间:2016-06-06 05:28:29

标签: java android android-layout arraylist

java代码         包 “”;

import yuku.ambilwarna.AmbilWarnaDialog;

/**
 * Created by pc-4 on 5/31/2016.
 */
public class EditWindow extends ActionBarActivity implements View.OnClickListener {

    private static final int SELECT_FILE = 1;
    ImageView iv1,iv2,iv3,iv4,iv5,iv6,iv7,iv8,iv9,iv10,iv11,iv12;
    Bitmap myBitmap;

    ArrayList<Integer> vericalArrayList = new ArrayList<Integer>();


    private LinearLayout hv;

    int color = 0xffffff00;
    LinearLayout linear_popup;
    Context context = this;
    String name, meaning;
    int j = 0;
    LinearLayout[] linearlayout;
    LinearLayout ll_main;
    String[] items;

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

        FinBtViewIds();
        ll_main.setBackgroundResource(R.drawable.page_back_ground);
        linear_popup = (LinearLayout) findViewById(R.id.linear_popup);
        Bundle bundle = getIntent().getExtras();
        name = bundle.getString("name");
        meaning = bundle.getString("meaning");
        j = name.length();
        linearlayout = new LinearLayout[j];
        items = meaning.split("\\s+");


             verical_linear();

    }



    private void galleryIntent() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
    }

    private void onSelectFromGalleryResult(Intent data) {
        Bitmap bm = null;
        if (data != null) {
            try {
                bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(EditWindow.this, "FAILES TO SET PIC", Toast.LENGTH_SHORT).show();
            }
            Drawable drawable = (Drawable) new BitmapDrawable(getResources(), bm);
            LinearLayout ll_main = (LinearLayout) findViewById(R.id.linear);
            ll_main.setBackground(drawable);
        }

    }

    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_background:
                linear_popup.setVisibility(View.GONE);
                hv.setVisibility(View.VISIBLE);
                break;

            case R.id.iv_gallery:
                linear_popup.setVisibility(View.GONE);
                hv.setVisibility(View.GONE);
                galleryIntent();
                break;
            case R.id.iv_text:
                linear_popup.setVisibility(View.GONE);
                hv.setVisibility(View.GONE);
                openDialog(true);
                break;

            case R.id.iv_edit_back:
                linear_popup.setVisibility(View.GONE);
                hv.setVisibility(View.GONE);
                onBackPressed();
                super.onBackPressed();
                break;

            case R.id.iv_edit_done:
                linear_popup.setVisibility(View.GONE);
                hv.setVisibility(View.GONE);
                ll_main.post(new Runnable() {
                    public void run() {

                        //take screenshot
                        myBitmap = captureScreen(ll_main);

                        Toast.makeText(getApplicationContext(), "Screenshot captured..!", Toast.LENGTH_LONG).show();

                        try {
                            if(myBitmap!=null){
                                //save image to SD card
                                saveImage(myBitmap);
                                Intent i=new Intent(EditWindow.this,SaveActivity.class);
                                startActivity(i);
                            }
                            Toast.makeText(getApplicationContext(), "Screenshot saved..!", Toast.LENGTH_LONG).show();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }
                });

                break;

            case R.id.iv_style:
                hv.setVisibility(View.GONE);
                if (linear_popup.getVisibility() == View.VISIBLE)
                    linear_popup.setVisibility(View.GONE);

                else {
                    linear_popup.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.tv_horizontal:
                hv.setVisibility(View.GONE);
                horizontal_linear();
                linear_popup.setVisibility(View.GONE);
                break;
            case R.id.tv_vertical:
                hv.setVisibility(View.GONE);
                verical_linear();
                linear_popup.setVisibility(View.GONE);
                break;
        }
    }


    public void verical_linear() {
        ll_main.setOrientation(LinearLayout.VERTICAL);
        ll_main.setGravity(Gravity.CENTER);
        ll_main.setVisibility(View.VISIBLE);
        ll_main.removeAllViews();

        int j = name.length();
        final LinearLayout[] linearlayout = new LinearLayout[j];
        String[] items = meaning.split("\\s+");

        for (int i = 0; i < j; i++) {

            LinearLayout parent = new LinearLayout(this);

            LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            param.weight = 1;
            parent.setLayoutParams(param);
            parent.setOrientation(LinearLayout.HORIZONTAL);

            TextView tv = new TextView(this);
            int text_id;
            Random r = new Random();
            text_id = r.nextInt();
            if (text_id < 0) {
                text_id = text_id - (text_id * 2);
            }
            tv.setId(text_id);
            Toast.makeText(EditWindow.this, String.valueOf(text_id), Toast.LENGTH_SHORT).show();
            vericalArrayList.add(text_id);

            Character c = name.charAt(i);
            tv.setText(c.toString().toUpperCase());
            Typeface face = Typeface.createFromAsset(getAssets(), "font/a.TTF");
            tv.setTypeface(face);
            tv.setTextSize(60);

            TextView t2 = new TextView(this);
            t2.setGravity(Gravity.END | Gravity.CENTER);
            t2.setTypeface(Typeface.DEFAULT);

            t2.setSingleLine(true);
            t2.setMaxLines(2);
            t2.setTextSize(20);
            t2.setTypeface(face);
            t2.setText(items[i]);
            parent.addView(tv);
            parent.addView(t2);
            linearlayout[i] = parent;
            ll_main.addView(parent);
        }

    }

    public void horizontal_linear() {

        ll_main.setOrientation(LinearLayout.HORIZONTAL);
        ll_main.setGravity(Gravity.CENTER);
        ll_main.setVisibility(View.VISIBLE);
        ll_main.removeAllViews();


        for (int i = 0; i < j; i++) {

            LinearLayout parent = new LinearLayout(this);
            LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            param.weight = 1;
            parent.setLayoutParams(param);
            parent.setOrientation(LinearLayout.VERTICAL);

            TextView tv = new TextView(this);
            int text_id;
            tv.setTag(tv.getId());
/*
            Random r = new Random();
            text_id = r.nextInt();
            if (text_id < 0) {
                text_id = text_id - (text_id * 2);
            }*/
            tv.setGravity(Gravity.CENTER);
            Character c = name.charAt(i);
            tv.setText(c.toString().toUpperCase());
            Typeface face = Typeface.createFromAsset(getAssets(), "font/a.TTF");
            tv.setTypeface(face);
            tv.setTextSize(60);

            TextView t2 = new TextView(this);
          /*  int text_id2;
            Random r1 = new Random();
            text_id2 = r1.nextInt();
            if (text_id2 < 0) {
                text_id2 = text_id2 - (text_id2 * 2);
            }*/
            t2.setTag(t2.getId());
            t2.setGravity(Gravity.CENTER);
            t2.setTypeface(Typeface.DEFAULT);
            t2.setSingleLine(true);

            t2.setMaxLines(1);
            t2.setTypeface(face);
            t2.setText(items[i]);
            int id=t2.getId();
            int id1=tv.getId();
            vericalArrayList.add(id);
            vericalArrayList.add(id1);
            Toast.makeText(EditWindow.this, String.valueOf("1"+id), Toast.LENGTH_SHORT).show();
            Toast.makeText(EditWindow.this, String.valueOf("2"+id1), Toast.LENGTH_SHORT).show();

            parent.addView(tv);
            parent.addView(t2);
            linearlayout[i] = parent;
            ll_main.addView(parent);


        }
    }


    public void FinBtViewIds() {
        final ImageView tv_horizontal;
        final ImageView tv_vertical;
        final ImageView iv_edit_back;
        final ImageView iv_done;
        final ImageView iv_gallery;
        final ImageView iv_background;
        final ImageView iv_style;
        final ImageView iv_text;

        iv1=(ImageView)findViewById(R.id.iv1);
        iv2=(ImageView)findViewById(R.id.iv2);
        iv3=(ImageView)findViewById(R.id.iv3);
        iv4=(ImageView)findViewById(R.id.iv4);
        iv5=(ImageView)findViewById(R.id.iv5);
        iv6=(ImageView)findViewById(R.id.iv6);
        iv7=(ImageView)findViewById(R.id.iv7);
        iv8=(ImageView)findViewById(R.id.iv8);
        iv9=(ImageView)findViewById(R.id.iv9);
        iv10=(ImageView)findViewById(R.id.iv10);
        iv11=(ImageView)findViewById(R.id.iv11);
        iv12=(ImageView)findViewById(R.id.iv12);
        ll_main = (LinearLayout) findViewById(R.id.linear);
        hv = (LinearLayout) findViewById(R.id.hv);
        iv_edit_back = (ImageView) findViewById(R.id.iv_edit_back);
        tv_horizontal = (ImageView) findViewById(R.id.tv_horizontal);
        tv_vertical = (ImageView) findViewById(R.id.tv_vertical);
        iv_done = (ImageView) findViewById(R.id.iv_edit_done);
        iv_gallery = (ImageView) findViewById(R.id.iv_gallery);
        iv_background = (ImageView) findViewById(R.id.iv_background);
        iv_style = (ImageView) findViewById(R.id.iv_style);
        iv_text = (ImageView) findViewById(R.id.iv_text);
        iv_edit_back.setOnClickListener(this);
        iv_done.setOnClickListener(this);
        iv_gallery.setOnClickListener(this);
        iv_background.setOnClickListener(this);
        iv_style.setOnClickListener(this);
        iv_text.setOnClickListener(this);
        tv_horizontal.setOnClickListener(this);
        tv_vertical.setOnClickListener(this);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == SELECT_FILE)
                onSelectFromGalleryResult(data);
        }
    }

    void openDialog(boolean supportsAlpha) {
        AmbilWarnaDialog dialog = new AmbilWarnaDialog(EditWindow.this, color, supportsAlpha, new AmbilWarnaDialog.OnAmbilWarnaListener() {
            @Override
            public void onOk(AmbilWarnaDialog dialog, int color) {
                //Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();
                EditWindow.this.color = color;
                displayColor();
            }

            @Override
            public void onCancel(AmbilWarnaDialog dialog) {
                // Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show();
    }

    void displayColor() {
        for (int i = 0; i < vericalArrayList.size(); i++) {
            String id = vericalArrayList.get(i).toString();
            TextView text = (TextView) findViewById(vericalArrayList.get(i));
            text.setTextColor(color);
            Toast.makeText(EditWindow.this, id + "/n" + i, Toast.LENGTH_SHORT).show();
        }
    }

    public void Onclick(View v)
    {
        switch (v.getId())
        {
            case R.id.iv1:
                ll_main.setBackgroundResource(R.drawable.n1);
                break;
            case R.id.iv2:
                ll_main.setBackgroundResource(R.drawable.n2);
                break;
            case R.id.iv3:
                ll_main.setBackgroundResource(R.drawable.n3);
                break;
            case R.id.iv4:
                ll_main.setBackgroundResource(R.drawable.n4);
                break;
            case R.id.iv5:
                ll_main.setBackgroundResource(R.drawable.n5);
                break;
            case R.id.iv6:
                ll_main.setBackgroundResource(R.drawable.n6);
                break;
            case R.id.iv7:
                ll_main.setBackgroundResource(R.drawable.n7);
                break;
            case R.id.iv8:
                ll_main.setBackgroundResource(R.drawable.n8);
                break;
            case R.id.iv9:
                ll_main.setBackgroundResource(R.drawable.n9);
                break;
            case R.id.iv10:
                ll_main.setBackgroundResource(R.drawable.n10);
                break;
            case R.id.iv11:
                ll_main.setBackgroundResource(R.drawable.n11);
                break;
            case R.id.iv12:
                ll_main.setBackgroundResource(R.drawable.n12);
                break;

        }

    }
    public static void saveImage(Bitmap bitmap) throws IOException{

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);
        File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.png");
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
    }
    public static Bitmap captureScreen(View v) {

        Bitmap screenshot = null;
        try {

            if(v!=null) {

                screenshot = Bitmap.createBitmap(v.getMeasuredWidth(),v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(screenshot);
                v.draw(canvas);
            }

        }catch (Exception e){
            Log.d("ScreenShotActivity", "Failed to capture screenshot because:" + e.getMessage());
        }

        return screenshot;
    }
}

XML

<LinearLayout
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:background="@color/white"
    android:orientation="horizontal"
    android:padding="@dimen/value_10"
    android:visibility="visible">


</LinearLayout>

我没有发布导入 整个XML DOENOT在这里很重要所以我只在这里玩单线性布局,所以只添加了

  

已添加完整代码

错误    java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setTextColor(int)'

1 个答案:

答案 0 :(得分:1)

当您将所有TextView ID存储在数组列表中时,您唯一需要做的就是将该视图转换为TextView并设置文本颜色。

TextView text = (TextView) findViewById(integerArrayList.get(i)); 
text.setTextColor(Color.RED);