从View类设置TextView Text不从Activity

时间:2013-09-19 12:12:26

标签: android android-activity textview android-custom-view

这是我的Activity类。在这里,我有一个Textview 我想从View Class设置TextView。

public class TestApp extends Activity 
{

  TextView   NameTxtView;
  CustomView  view;

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.Main); 
         view                       = (CustomView)findViewById(R.id.customview);    

        NameTxtView  = (TextView) findViewById(R.id.nameTxtxVw);

   }

}

这是我的View类。在这里,我想设置TextView文本。我无法在Activity上设置此文本。因为我在View类上获得了价值。

public  class CustomView extends View
{
    public CustomView(Context context, AttributeSet attrs) 
    {
          super(context, attrs); 
    } 

    @Override
    public void onDraw(Canvas canvas) 
    {
          NameTxtView.settext("Test");

    }
}

任何想法怎么做?

谢谢

3 个答案:

答案 0 :(得分:5)

这样做

<强> TestApp.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.example.demo.CustomView.CustmViewListener;

public class TestApp extends Activity 
{

  TextView   NameTxtView;
  CustomView  view;

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.Main); 
         view   = (CustomView)findViewById(R.id.customview);    

        NameTxtView  = (TextView) findViewById(R.id.nameTxtxVw);

        view.setCustmViewListener(new CustmViewListener() {

            @Override
            public void onUpdateValue(String updatedValue) {
                NameTxtView.setText(updatedValue);
            }
        });

   }

}

<强> CustomView.java

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;

public class CustomView extends View {

    CustmViewListener custmViewListener;

    public CustmViewListener getCustmViewListener() {
        return custmViewListener;
    }

    public void setCustmViewListener(CustmViewListener custmViewListener) {
        this.custmViewListener = custmViewListener;
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onDraw(Canvas canvas) {
        if (getCustmViewListener() != null) {
            getCustmViewListener().onUpdateValue("passYourValueHere");
        }

    }

    public interface CustmViewListener {
        void onUpdateValue(String updatedValue);
    }
}

答案 1 :(得分:0)

试试这样: 在CustomView构造函数中调用LayoutInflaterService并为您的Layout&amp; amp; TextView:

LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
final RelativeLayout yourLayout = (RelativeLayout) layoutInflater.inflate(R.layout.dialog_menu, null);
TextView myText = (TextView) yourLayout.findViewById(R.id.nameTxtxVw);
myText.setText("");

答案 2 :(得分:0)

你的问题是活动类有一个上下文而你的第二个没有。要解决这个问题,请创建一个全局变量:static Activity activity并在构造函数中初始化它。

然后致电:new CustomClass(ActivityClass.this)

接下来,只要您想要执行与活动相关的操作,只需将activity.放在内容

之前