2015-07-02 21 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

相关问题