[英]How to setVisibility of a TextView in a Dialogue Box, called from outside the MainActivity Thread?
I have a textview in a dialogue box and I want to toggle the visibility from a class which is outside the MainActivity Thread. 我在对话框中有一个textview,我想从一个MainActivity Thread之外的类切换可见性。
In the MainActivity,I have the Dialog box: 在MainActivity中,我有对话框:
addDevDialogue = new Dialog(this);
addDevDialogue.setContentView(R.layout.add_device_dialogue);
public void addDeviceDialogue ()
{
addDevDialogue.setTitle("Add SAndTerm Device:");
addDevDialogue.setCancelable(false);
addDevDialogue.show();
incorrectIP = (TextView)addDevDialogue.findViewById(R.id.rongtxt);
incorrectIP.setVisibility(View.INVISIBLE);
}
And I have another class where if a particular condition meets, I want to change the visibility of the TextView which is in the dialogue box. 我有另一个类,如果特定条件满足,我想改变对话框中TextView的可见性。 For reference sake I ll put up the class from where I want to toggle the visibility. 为了参考起见,我将从我想要切换可见性的地方开始上课。
The Class: 班级:
public class IP_Validation {
Context contxt;
public IP_Validation(Context context)
{
contxt = context;
}
public void change_state()
{
//toggle the TextView Visibility from here
}
}
Could you tell me a proper way to access the textview from a class which is outside the MainActivity? 你能告诉我一个从MainActivity之外的类访问textview的正确方法吗?
In your activity in onCreate create new object: 在onCreate的活动中创建新对象:
textView = (TextView) findViewById(R.id.textView);
TestNonActivityClass test = new TestNonActivityClass(textView);
test.doSomething();
Next, create constructor in your nonActivity class, like this: 接下来,在nonActivity类中创建构造函数,如下所示:
public class TestNonActivityClass {
private TextView textViewInNonActivityClass; //textview
public TestNonActivityClass(TextView textView){ //constructor
this.textViewInNonActivityClass = textView;
}
public void changeText(){ //some method..
textViewInNonActivityClass.setText("Changed");
textViewInNonActivityClass.setVisibility(View.INVISIBLE);
}
}
If you change state of textViewInNonActivityClass, it should change on the screen.. 如果你改变textViewInNonActivityClass的状态,它应该在屏幕上改变..
Create a handler in your Activity 在Activity中创建一个处理程序
Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what == 1){ // set toggle visible or whatever } } }
Pass the handler to your IP_Validation 将处理程序传递给您的IP_Validation
Handler handler; public IP_Validation(Context context , Handler handler) { contxt = context; this.handler = handler; }
When you need to change the visibility , use 当您需要更改可见性时,请使用
handler.sendEmptyMessage(1);
Simplest form of solution would to make this change: 最简单的解决方案是进行此更改:
public void addDeviceDialogue ()
{
runOnUiThread(new Runnable() {
@Override
public void run() {
addDevDialogue.setTitle("Add SAndTerm Device:");
addDevDialogue.setCancelable(false);
addDevDialogue.show();
incorrectIP = (TextView)addDevDialogue.findViewById(R.id.rongtxt);
incorrectIP.setVisibility(View.INVISIBLE);
}
});
}
and call the method like this: 并调用这样的方法:
public void change_state()
{
((MainActivity) context).addDeviceDialogue();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.