2015-07-02 19 views
1

我編寫了下面的代碼,並且我想要顯示其具有圓的區域的圓和其內容。但是當我運行下面的代碼時,它只顯示圓圈,它不能同時顯示圓圈和TextView的內容。解決辦法是什麼?在一個活動中顯示TextView的圓和內容

Draw.java

public class Draw extends View { 
Paint paint = new Paint(); 
Draw(Context context) { 
    super(context); 
} 
public void onDraw(Canvas canvas) { 
    paint.setColor(Color.BLUE); 
    canvas.drawCircle(120,120,40,paint); 
} 
} 

Cal.java

public class Cal extends View { 
    Cal(Context context){ 
     super(context); 
    } 
    public double result; 
    double parameter = (Math.pow(40,2)) * 3.14; 
    public void cal(){ 
     result = Math.sqrt(parameter); 

    } 
} 

MainActivity.java

public class MainActivity extends Activity{ 
    Draw draw; 
    Cal cal; 
    TextView textView; 
    public void onCreate(Bundle s){ 
     super.onCreate(s); 
     setContentView(R.layout.activity_main); 
textView = (TextView) findViewById(R.id.textView); 
     cal = new Cal(this); 
     cal.cal(); 
     textView.setText(""+ cal.result); 
draw = new Draw(this); 
     setContentView(draw); 
    } 
} 

回答

0

enter image description hereXML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/linearLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
</LinearLayout> 

與完成單一Activtiy

public class MainActivity extends Activity { 
Draw draw; 
Cal cal; 
TextView textView; 
LinearLayout linearLayout; 

public void onCreate(Bundle s) { 
    super.onCreate(s); 
    setContentView(R.layout.test1); 

    linearLayout = (LinearLayout) findViewById(R.id.linearLayout); 
    cal = new Cal(this); 
    cal.cal(); 

    textView = new TextView(getApplicationContext()); 
    textView.setText("" + cal.result); 
    textView.setTextColor(Color.RED); 

    draw = new Draw(this); 
    linearLayout.addView(textView); 
    linearLayout.addView(draw); 
} 

public class Draw extends View { 
    Paint paint = new Paint(); 

    Draw(Context context) { 
     super(context); 
    } 

    public void onDraw(Canvas canvas) { 
     paint.setColor(Color.BLUE); 
     canvas.drawCircle(120, 120, 40, paint); 
    } 
} 

public class Cal extends View { 
    Cal(Context context) { 
     super(context); 
    } 

    public double result; 
    double parameter = (Math.pow(40, 2)) * 3.14; 

    public void cal() { 
     result = Math.sqrt(parameter); 

     } 
    } 
} 
+0

謝謝。這很有用。但正如你在上面的代碼中看到的,我們有一個Cal類,它有一個值(結果變量)。我希望在一個活動中顯示這個變量,而不會在代碼中發生重大變化。是否有可能? – developer

+0

你想cal.result添加到圈子? –

+0

我想在圓上添加cal.result。但沒關係。只有我想在活動中顯示結果變量。 – developer

0

textView.setText(""+ cal.result);更改此行像下面

textview.setBackground(cal.result); 
textview.setText("your text goes here"); 

(OR)

實在是太簡單的使用<layer-list>繪製圓並將其設置爲你的TextView的背景

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item> 
     <shape android:shape="oval" > 
      <solid android:color="#222222" /> 

      <size 
       android:height="28dp" 
       android:width="28dp" /> 
     </shape> 
    </item> 

</layer-list> 

您必須創建.xml文件in drawable

+0

謝謝。但我不想使用.xml文件來做這件事。 – developer

+0

@developer嘗試我的新更新 – Madhu

+0

「setBackground」無法接受「cal.result」 – developer

相關問題