使用android中的颜色选择器更改textview的文本颜色和背景颜色

nov*_*oid 0 android color-picker textview android-layout

如何TextView在android中更改使用颜色选择器的文本颜色和背景颜色.添加具有更改文本颜色的功能的注释和从颜色选择器中选择颜色的背景.

注意

Nar*_*dra 9

  • 下载此项目导入它.颜色选择器

  • 右键单击项目--->属性---> android --->添加单击并添加下载项目.

  • 创建新项目
  • 布局注意:使用下载的项目res文件夹---> drawable中的颜色选择器图像

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
    
         <EditText
            android:id="@+id/txNote"
            android:layout_width="200dip"
            android:layout_height="200dip"
            android:layout_centerInParent="true"
            android:text="@string/hello_world" />
    
         <ImageView
             android:id="@+id/rightColorPicker"
             android:layout_width="@dimen/ambilwarna_hueWidth"
             android:layout_height="@dimen/ambilwarna_hsvHeight"
             android:layout_alignParentRight="true"
             android:layout_alignTop="@+id/txNote"
             android:scaleType="fitXY"
             android:src="@drawable/ambilwarna_hue" />
    
         <ImageView
             android:id="@+id/leftColorPicker"
             android:layout_width="@dimen/ambilwarna_hueWidth"
             android:layout_height="@dimen/ambilwarna_hsvHeight"
             android:layout_alignParentLeft="true"
             android:layout_alignTop="@+id/txNote"
             android:scaleType="fitXY"
             android:src="@drawable/ambilwarna_hue" />
    
    </RelativeLayout>
    
    Run Code Online (Sandbox Code Playgroud)
    • 活动

      public class MainActivity extends Activity implements OnTouchListener {
      
      TextView txtNote;
      ImageView rightColorPicker,leftColorPicker;
      private int mAppWidgetId = 0 ;
      public static boolean flag;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          txtNote=(TextView)findViewById(R.id.txNote);
          rightColorPicker=(ImageView)findViewById(R.id.rightColorPicker);
          leftColorPicker=(ImageView)findViewById(R.id.leftColorPicker);
          rightColorPicker.setOnTouchListener(this);
          leftColorPicker.setOnTouchListener(this);
          Intent intent = getIntent();
          Bundle extras = intent.getExtras();
          if (extras != null) {
      
              mAppWidgetId = extras.getInt(
                      AppWidgetManager.EXTRA_APPWIDGET_ID, 
                      AppWidgetManager.INVALID_APPWIDGET_ID);
      
          }
      
      }
      
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          switch (v.getId()) {
          case R.id.rightColorPicker:
              colorPicker();
              flag=true;
              break;
          case R.id.leftColorPicker:
              colorPicker();
              flag=false;
              break;
          default:
              break;
          }
      
          return false;
      }
      
      public void colorPicker() {
      
      //      initialColor is the initially-selected color to be shown in the rectangle on the left of the arrow.
      //      for example, 0xff000000 is black, 0xff0000ff is blue. Please be aware of the initial 0xff which is the alpha.
      ColorPickerDialog dialog = new ColorPickerDialog(this, 0xff0000ff, new OnAmbilWarnaListener() {
      
          // Executes, when user click Cancel button
          @Override
          public void onCancel(ColorPickerDialog dialog){
          }
      
          // Executes, when user click OK button
          @Override
          public void onOk(ColorPickerDialog dialog, int color) {
              // Create an Intent to launch WidgetConfigurationActivity screen
              Intent intent = new Intent(getBaseContext(), MainActivity.class);
      
              intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
      
              // This is needed to make this intent different from its previous intents 
              intent.setData(Uri.parse("tel:/"+ (int)System.currentTimeMillis()));
      
              // Creating a pending intent, which will be invoked when the user
              // clicks on the widget
              PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, 
                      intent, PendingIntent.FLAG_UPDATE_CURRENT);                     
      
              // Getting an instance of WidgetManager
              AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getBaseContext());
      
              if (flag) {
                   txtNote.setBackgroundColor(color);
      
              } else {
                  txtNote.setTextColor(color);
              }
      
      // // Instantiating the class RemoteViews with widget_layout
            RemoteViews views = new 
            RemoteViews(getBaseContext().getPackageName(), R.layout.activity_main);
      //  
      // // Setting the background color of the widget
            views.setInt(R.id.txNote, "setBackgroundColor", color);                        
      //                          
      // //  Attach an on-click listener to the clock
           views.setOnClickPendingIntent(R.id.txNote,pendingIntent);
      
              // Tell the AppWidgetManager to perform an update on the app widget
              appWidgetManager.updateAppWidget(mAppWidgetId, views);                        
      
              // Return RESULT_OK from this activity
              Intent resultValue = new Intent();
              resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
              setResult(RESULT_OK, resultValue);
              //finish();
          }
      });
      dialog.show();
      }
      }
      
      Run Code Online (Sandbox Code Playgroud)