程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#中volatile與lock用法

C#中volatile與lock用法

編輯:C#入門知識

C#中volatile與lock用法。本站提示廣大學習愛好者:(C#中volatile與lock用法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中volatile與lock用法正文


有許多情形下,我們只需應用好Android給我供給好的控件,經由結構奇妙的聯合在一路,就是一個新的控件,我稱之為“自界說組合控件”。

那末,這類自界說組合控件在甚麼情形下用呢?或許年夜家在做項目時刻會發明,某些結構會被反復的應用,統一個結構的XML代碼塊會被反復的復制黏貼屢次,如許會形成代碼構造凌亂不說,代碼量也會增年夜,各類控件都須要在Java代碼中被聲名和處置響應的邏輯,任務量實在不小,所以,必需要找到一個公道的“偷懶”的方法,開動腦經去怎樣簡化以上說的不用要的費事。上面看一張圖,就一個簡略的結構,我們就此圖來完成一個簡略的自界說組合控件。

https://www.aspphp.online/bianchen/UploadFiles_4619/201707/2017072809363185.jpg (478×707)

從下面的圖來剖析,我們可以看到,這個結構外面是沒有“全新”的控件的,用的都是Android體系原生的控件。熟習Android界面結構的人,確定認為這類結構真是小Case,太簡略了,分分鐘便可以寫完。因而上面就是某一個條目標結構代碼:

<!--?xml version=1.0 encoding=utf-8?-->
<relativelayout android:background="@drawable/selector_blue" android:id="@+id/rl_show_address" android:layout_height="60dip" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">
 
  <textview android:id="@+id/tv_title" android:layout_height="wrap_content" android:layout_marginleft="5dip" android:layout_margintop="1dip" android:layout_width="wrap_content" android:text="這是題目" android:textcolor="#000000" android:textsize="20sp">
 
  <textview android:id="@+id/tv_desc" android:layout_below="@id/tv_title" android:layout_height="wrap_content" android:layout_marginleft="6dip" android:layout_margintop="1dip" android:layout_width="wrap_content" android:text="這是描寫內容" android:textcolor="#99ff0000" android:textsize="14sp">
 
  <checkbox android:clickable="false" android:focusable="false" android:id="@+id/cb_status" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_height="wrap_content" android:layout_width="wrap_content">
   
  <!-- 加一條朋分線 -->
  <view android:background="#000000/" android:layout_alignbottom="@id/cb_status" android:layout_alignparentbottom="true" android:layout_height="0.2dip" android:layout_margintop="7dip" android:layout_width="match_parent">
 
</view></checkbox></textview></textview></relativelayout>

可以看到,這類結構確切相當的簡略。然則,這時候候產物司理告知你,需求改了,我們須要在這個界面再加一個如許的條目,因而你認為,小意思,Ctrl+C,Ctrl+V,輕松弄定,然後改一下控件的id,在Java代碼中findviewbyid(id),加一段邏輯代碼,弄完出工。沒想到這時候候產物又來了,需求改了,這裡須要加10個如許的結構,因而你...固然,這時候候再Ctrl+C,Ctrl+V是不適合的,任務量就顯得很年夜了,即便你不嫌費事的話,照樣做了,你料不到產物會再來,誰人給我刪失落幾個,誰人再加上幾個,是否是要瘋了。
 

或許,我們可以相出一個偷懶的辦法來呢。經由過程剖析下面的結構,可以發明,結構上每個子條目是不變的,結構完整一樣,獨一在變更的是,白色的TextView上的文本跟著CheckBox的狀況再轉變著,而這類變更,我們能否可以想方法抽取到某個辦法中呢,謎底是確定能的。我們可以將這類子條目標結構一次性封裝到一個Java類中,每次挪用這個控件的時刻,事前設定各類屬性數據便可,這裡觸及到了自界說屬性了。剖析一下這個屬性集該怎樣界說,從下面的圖片可以看出,控件上須要設置的內容分離是,下面TextView的題目,還有上面TextView的描寫信息,且描寫信息是依據CheckBox的狀況產生轉變的,所以這兩種狀況(true或false)都須要被界說到屬性集裡去,因而屬性集就有了。

在工程下的res/values目次下,新建attrs.xml文件,界說以下屬性集:

<!--?xml version=1.0 encoding=utf-8?-->
<resources>
 
  <declare-styleable name="combinationView">
    </attr>
    </attr>
    </attr>
  </declare-styleable>
 
</resources>

界說好了屬性集了,接上去我們就須要界說一個Java類,來襯著這段結構,解析這個屬性集,而且對象供給修正控件狀況的辦法,已到達復用的後果。成績來了,我們界說的這個Java類須要繼續哪一個類呢?在這裡,我們不用斟酌View了,由於這裡不是全新自界說控件,不須要onMessure和onDraw去丈量去畫一個視圖。那末ViewGroup呢?我們也不用用這個類,由於這裡的結構是給定好的,不須要應用onLayout給子控件設置顯示的地位。那末,該繼續甚麼呢?我們可以想象一下ViewGroup的子類是否是可以呢?完成自界說控件的除繼續View和ViewGroup以外,還可以直接繼續Android已有的控件停止修正,這個用面向對象的思惟,應當不難想象吧。因為,該結構文件用的絕對結構RelativeLayout,我們想固然可以自界說Java類去繼續這個RelativeLayout,RelativeLayout裡供給一些參數和辦法便利我們去完成子控件的結構。然則,我們這裡直接在子控件結構曾經寫好了,不須要應用RelativeLayout供給的參數和辦法來結構了。所以,招致了,即便不去繼續RelativeLayout,而改成LinearLayout,FrameLayout...也是可以的,只需這個結構類是ViewGroup的子類就行。以下是這個自界說組合控件的完成代碼:

package com.example.combinationview;
 
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
public class CombinationView extends RelativeLayout {
 
  private TextView tv_title;
  private TextView tv_desc;
  private CheckBox cb_status;
  // 定名空間,在援用這個自界說組件的時刻,須要用到
  private String namespace = http://schemas.android.com/apk/res/com.example.combinationview;
  // 題目
  private String title;
  // 被選中的描寫
  private String desc_on;
  // 未被選中的描寫
  private String desc_off;
 
  public CombinationView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // 將自界說組合控件的結構襯著成View
    View view = View.inflate(context, R.layout.layout_combinationview, this);
    tv_title = (TextView) view.findViewById(R.id.tv_title);
    tv_desc = (TextView) view.findViewById(R.id.tv_desc);
    cb_status = (CheckBox) view.findViewById(R.id.cb_status);
 
    title = attrs.getAttributeValue(namespace, title);
    desc_on = attrs.getAttributeValue(namespace, desc_on);
    desc_off = attrs.getAttributeValue(namespace, desc_off);
    System.out.println(title + : + desc_on + : + desc_off);
    // 初始化到子控件
    if (title != null) {
      tv_title.setText(title);
    }
    if (desc_off != null) {
      tv_desc.setText(desc_off);
    }
  }
 
  /**
   * 斷定能否被選中
   * 
   * @return
   */
  public boolean isChecked() {
    return cb_status.isChecked();
  }
 
  /**
   * 設置選中的狀況
   * 
   * @param isChecked
   */
  public void setChecked(boolean isChecked) {
    cb_status.setChecked(isChecked);
    if (isChecked) {
      tv_desc.setText(desc_on);
    } else {
      tv_desc.setText(desc_off);
    }
  }
 
}

代碼很簡略,起首繼續RelativeLayout,復寫其結構辦法,在結構辦法中先襯著結構的視圖,然後讀取屬性集的屬性,將默許顯示的屬性顯示到結構上的子控件上便可。別的,還要對外供給一個斷定狀況的辦法isChecked()來斷定該控件能否被選中了,供給一個設置狀況的辦法setChecked(boolean),用來轉變狀況。PS:為了驗證我下面的一段話,讀者可以將繼續RelativeLayout,改成繼續LinearLayout或許繼續FrameLayout,運轉嘗嘗看,也是可以完成的。
 

上面是援用這個自界說組合控件的辦法,起首須要在Activity的結構文件中界說出來:

<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:example="http://schemas.android.com/apk/res/com.example.combinationview">
 
  <com.example.combinationview.combinationview android:id="@+id/cv_first" android:layout_height="wrap_content" android:layout_width="match_parent" example:desc_off="我是未被選中的描寫1" example:desc_on="我是被選中的描寫1" example:title="我是題目1">
  </com.example.combinationview.combinationview>
 
  <com.example.combinationview.combinationview android:id="@+id/cv_second" android:layout_height="wrap_content" android:layout_width="match_parent" example:desc_off="我是未被選中的描寫2" example:desc_on="我是被選中的描寫2" example:title="我是題目2">
  </com.example.combinationview.combinationview>
 
  <com.example.combinationview.combinationview android:id="@+id/cv_third" android:layout_height="wrap_content" android:layout_width="match_parent" example:desc_off="我是未被選中的描寫3" example:desc_on="我是被選中的描寫3" example:title="我是題目3">
  </com.example.combinationview.combinationview>
 
  <com.example.combinationview.combinationview android:id="@+id/cv_fourth" android:layout_height="wrap_content" android:layout_width="match_parent" example:desc_off="我是未被選中的描寫4" example:desc_on="我是被選中的描寫4" example:title="我是題目4">
  </com.example.combinationview.combinationview>
 
</linearlayout>

起首在下面界說了四個自界說組合控件,年夜家可以看到,代碼精簡多了不是?!須要留意的處所:這裡援用了自界說的屬性集,所以在結構節點上必需要加上定名空間

xmlns:example=http://schemas.android.com/apk/res/com.example.combinationview

個中,example是定名空間的稱號,是隨意率性取的,然則必需在控件中援用屬性的稱號分歧,否則會報錯。前面的一串是標明屬性集的途徑,前半部門是固定的,最初一個“/”前面的內容必需是工程的包名,不然報錯。
 

上面是Activity外面的營業邏輯代碼,沒甚麼好說的

package com.example.combinationview;
 
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
 
public class MainActivity extends Activity implements OnClickListener {
 
  private CombinationView cv_first;
  private CombinationView cv_second;
  private CombinationView cv_third;
  private CombinationView cv_fourth;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    cv_first = (CombinationView) findViewById(R.id.cv_first);
    cv_second = (CombinationView) findViewById(R.id.cv_second);
    cv_third = (CombinationView) findViewById(R.id.cv_third);
    cv_fourth = (CombinationView) findViewById(R.id.cv_fourth);
    cv_first.setOnClickListener(this);
    cv_second.setOnClickListener(this);
    cv_third.setOnClickListener(this);
    cv_fourth.setOnClickListener(this);
  }
 
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.cv_first:
      if (cv_first.isChecked()) {
        cv_first.setChecked(false);
      } else {
        cv_first.setChecked(true);
      }
      break;
    case R.id.cv_second:
      if (cv_second.isChecked()) {
        cv_second.setChecked(false);
      } else {
        cv_second.setChecked(true);
      }
      break;
    case R.id.cv_third:
      if (cv_third.isChecked()) {
        cv_third.setChecked(false);
      } else {
        cv_third.setChecked(true);
      }
      break;
    case R.id.cv_fourth:
      if (cv_fourth.isChecked()) {
        cv_fourth.setChecked(false);
      } else {
        cv_fourth.setChecked(true);
      }
      break;
    default:
      break;
    }
  }
 
}

好了,關於自界說組合控件就講完了,異常簡略,然則比擬經常使用。今後在項目用到時,想一想完成步調,自界說一種的組合的控件,用起來確切比擬便利,比純真的復制黏貼不只嵬峨上,並且進步代碼的復用性,簡化了代碼的構造和削減了代碼量。

上面再來看如許的一個完全的實例,比擬簡略,直接上代碼了:2016415142520403.jpg (1179×664)

package com.xiong.demo1; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 

public class MainActivity extends Activity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_activity); 
    TitleBarView titleBarView = (TitleBarView) findViewById(R.id.tbar_test); 
    titleBarView.getTextViewRigth().setVisibility(View.GONE); 
    titleBarView.setTitleBarChangerLiseter(new ItitleOnChangeLister() { 
      @Override 
      public void setLeftOnClickLister() { 
        finish(); 
      } 
 
      @Override 
      public void setRigthOnClickLister() { 
 
      } 
    }); 
  } 
 
} 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:xionglh="http://schemas.android.com/apk/res-auto" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
 
  <com.xiong.demo1.TitleBarView 
    android:id="@+id/tbar_test" 
    android:layout_width="match_parent" 
    android:layout_height="45dp" 
    xionglh:titleBar_center_text="首頁" 
    xionglh:titleBar_center_textColor="@android:color/black" 
    xionglh:titleBar_center_text_size="18sp" 
    xionglh:titleBar_left_bg="@mipmap/left_back" 
    xionglh:titleBar_right_text="平安中間" 
    xionglh:titleBar_right_text_size="12sp"/> 
</LinearLayout> 

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
  <declare-styleable name="TitleBar"> 
 
    <attr name="titleBar_center_text_size" format="dimension"/> 
    <attr name="titleBar_center_text" format="string"/> 
    <attr name="titleBar_center_textColor" format="color"/> 
 
    <attr name="titleBar_left_bg" format="reference"/> 
 
    <attr name="titleBar_right_text_size" format="dimension"/> 
    <attr name="titleBar_right_text" format="string"/> 
    <attr name="titleBar_right_textColor" format="color"/> 
 
  </declare-styleable> 
 
</resources> 

package com.xiong.demo1; 
 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Color; 
import android.util.AttributeSet; 
import android.util.TypedValue; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
 
public class TitleBarView extends RelativeLayout { 
 
  private ItitleOnChangeLister mItitleOnChangeLister; 
 
  private ImageView mImgLeft; 
  private TextView mTxtCenter, mTxtRigth; 
 
  private float mTitleCenterTextSize;//題目字體年夜小 
  private String mTitleCenterText;//題目文字 
  private int mTitleCenterTextColor;//題目色彩 
 
  private int mLeftBg;//右邊前往按鈕 
 
  private float mTitleRigthTextSize;//題目字體年夜小 
  private String mTitleRigthText;//題目文字 
  private int mTitleRigthColor;//題目色彩 
 
  public TitleBarView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    int defualtSize = (int) TypedValue.applyDimension( 
        TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()); 
    TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TitleBar); 
    mTitleCenterTextSize = typedArray.getDimension(R.styleable.TitleBar_titleBar_center_text_size, defualtSize); 
    mTitleCenterText = typedArray.getString(R.styleable.TitleBar_titleBar_center_text); 
    mTitleCenterTextColor = typedArray.getColor(R.styleable.TitleBar_titleBar_center_textColor, Color.RED); 
    mLeftBg = typedArray.getResourceId(R.styleable.TitleBar_titleBar_left_bg, R.mipmap.left_back); 
    mTitleRigthTextSize = typedArray.getDimension(R.styleable.TitleBar_titleBar_right_text_size, defualtSize); 
    mTitleRigthText = typedArray.getString(R.styleable.TitleBar_titleBar_right_text); 
    mTitleRigthColor = typedArray.getColor(R.styleable.TitleBar_titleBar_right_textColor, Color.RED); 
    typedArray.recycle(); 
    initView(); 
  } 
 
  private void initView() { 
    mTxtCenter = new TextView(getContext()); 
    mTxtCenter.setText(mTitleCenterText); 
    mTxtCenter.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleCenterTextSize); 
    mTxtCenter.setTextColor(mTitleCenterTextColor); 
    LayoutParams centerParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    centerParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE); 
    addView(mTxtCenter, centerParams); 
    mTxtRigth = new TextView(getContext()); 
    mTxtRigth.setText(mTitleRigthText); 
    mTxtRigth.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleRigthTextSize); 
    mTxtRigth.setTextColor(mTitleRigthColor); 
    LayoutParams rigthParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    rigthParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); 
    rigthParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); 
 
    addView(mTxtRigth, rigthParams); 
    mImgLeft = new ImageView(getContext()); 
    mImgLeft.setImageResource(mLeftBg); 
    LayoutParams leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    leftParams.setMargins(6, 0, 0, 0); 
    leftParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE); 
    addView(mImgLeft, leftParams); 
    View view = new View(getContext()); 
    view.setMinimumWidth(1); 
    view.setBackgroundColor(getResources().getColor(R.color.gray_767676)); 
    LayoutParams viewParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 1); 
    viewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    addView(view, viewParams); 
    mImgLeft.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        mItitleOnChangeLister.setLeftOnClickLister(); 
      } 
    }); 
 
    mTxtRigth.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        mItitleOnChangeLister.setRigthOnClickLister(); 
      } 
    }); 
 
  } 
 
  public void setTitleBarChangerLiseter(ItitleOnChangeLister ititleOnChangeLister) { 
    this.mItitleOnChangeLister = ititleOnChangeLister; 
  } 
 
 
  public ImageView getLeftImage() { 
    return mImgLeft; 
  } 
 
  public TextView getTextViewCenter() { 
    return mTxtCenter; 
  } 
 
  public TextView getTextViewRigth() { 
    return mTxtRigth; 
  } 
} 

package com.xiong.demo1; 

public interface ItitleOnChangeLister { 
 
 
  void setLeftOnClickLister(); 
  void setRigthOnClickLister(); 
 
} 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved