我找不到如何从textview
更改片段的Activity
。我有4个文件:
MainActivity.java
activity_main.xml
FragmentClass.java
frag_class.xml
frag_class.xml有textView,我想从MainActivity.java更改文本。 FragmentClass扩展了Fragment,这个片段显示在MainActivity中 FragmentClass有:
public void changeText(String text){
TextView t = (TextView) this.getView().findViewById(R.id.tView);
t.setText(text);
}
在MainActivity中我尝试了这个:
FragmentClass fc = new FragmentClass();
fc.changeText("some text");
但遗憾的是这段代码在fc.changeText("some text");
给了我NullPointerException
我还尝试使用以下方法直接从MainActivity更改文本:
TextView t = (TextView) this.getView().findViewById(R.id.tView);
t.setText(text);
失败。
[编辑] 完整代码为here
答案 0 :(得分:11)
您可以使用
找到Fragment的实例对于支持库,
YourFragment fragment_obj = (YourFragment)getSupportFragmentManager().
findFragmentById(R.id.fragment_id);
其他
YourFragment fragment_obj = (YourFragment)getFragmentManager().
findFragmentById(R.id.fragment_id);
然后在片段中创建一个更新TextView
的方法,并使用fragment_obj
调用该方法,
fragment_obj.updateTextView();
答案 1 :(得分:10)
从activity
到Fragment
Fragment Transaction
:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public static void changeFragmentTextView(String s) {
Fragment frag = getFragmentManager().findFragmentById(R.id.yourFragment);
((TextView) frag.getView().findViewById(R.id.textView)).setText(s);
}
}
答案 2 :(得分:5)
@Lalit答案是正确的,但我发现你不需要创建像fragment_obj.updateTextView();这样的函数。我将所有视图设置为类级别对象,并且能够直接更新textview。
fragmentRegister.textViewLanguage.setText("hello mister how do you do");
注意:如果您需要执行多个操作,那么可以使用函数。
答案 3 :(得分:1)
我以其他方式完成了它。我让我的应用程序像这样工作:
解雇我的应用后,我创建了HomeArag的MainActivity。在HomeFragment中,我有Button和TextView用于连接/显示BluetoothConnection的状态。
在HomeFragment中,我实现了Handler,用于从BluetoothService接收信息。收到消息后,我想更新TextView和Button文本。我在HomeFragment中使用获取TextView和Button的String参数的方法创建了公共接口。在onAttach(Activity a)中,我创建了mCallback对象来与活动进行交谈。
下一步是在MainActivity中实现此接口。从这个Activity我正在更新TextView和Button。一切都是这样的:
public interface ViewInterface{
public void onViewUpdate(String buttonTxt, String txtTxt);
}
@Override
public void onAttach(Activity a){
super.onAttach(a);
try{
mCallback = (ViewInterface)a;
}catch (ClassCastException e){
e.printStackTrace();
}
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
if(true) Log.i(CLASS_TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1) {
case BluetoothConnectionService.STATE_CONNECTED:
threadTextString = "Connected to: " + connectedDeviceName;
threadBtnString = "Disconnect";
mCallback.onViewUpdate(threadBtnString, threadTextString);
break;
case BluetoothConnectionService.STATE_CONNECTING:
threadTextString = "Connecting...";
mCallback.onViewUpdate(threadBtnString, threadTextString);
break;
case BluetoothConnectionService.STATE_NONE:
threadBtnString = "Connect";
threadTextString = "You're not connectedd";
mCallback.onViewUpdate(threadBtnString, threadTextString);
break;
}
private void updateBtn(Button btn, String data){
btn.setText(data);
Log.d(CLASS_TAG + "/" + "updateBtn", "Data: " + data);
}
private void updateTxt(TextView txt, String data){
txt.setText(data);
Log.d(CLASS_TAG + "/" + "updateTxt", "Data: " + data);
}
public void update(String buttonTxt, String txtTxt){
this.updateTxt(connectTxt, txtTxt);
this.updateBtn(connectButton, buttonTxt);
}
@Override
public void onViewUpdate(String buttonTxt, String txtTxt) {
HomeFragment homeFragment = (HomeFragment)getSupportFragmentManager().findFragmentById(R.id.frame_container);
if(homeFragment != null){
homeFragment.update(buttonTxt, txtTxt);
}
}
答案 4 :(得分:0)
If List contains 'addin1' then 'addin1' installed = true
使用这行代码来获得对文本字段的支持
public class Dcrypt extends Fragment {
Button butD;
ImageButton Dcopy;
EditText getPass;
TextView txtShow;