-5

在我的 Android 应用程序中,我将片段活动用于片段标签主机。在该片段活动中,我使用了一个编辑文本框,我无法在该编辑文本中编辑或输入文本。

该片段活动的代码是

package com.example.fragmenttabhost;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class FragmentTab extends android.support.v4.app.Fragment {

private Button btnTest;
private Activity context;
private EditText edtTest;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context=getActivity();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_layout, container, false);
    return v;
}
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    context=getActivity();
    btnTest=(Button)context.findViewById(R.id.button1);
    edtTest=(EditText)context.findViewById(R.id.editText1);
    edtTest.setText("Test text");
    final AlertDialog.Builder builder = new AlertDialog.Builder(context);

    builder.setTitle("Test");    
    btnTest.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String str=edtTest.getText().toString();
            builder.setMessage(str);
            builder.show();
        }
    });
  }
  }
4

1 回答 1

1

试试下面的代码

只需在onCreateView()方法上添加所有代码。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_layout, container, false);

    context=getActivity();
    btnTest=(Button)context.findViewById(R.id.button1);
    edtTest=(EditText)context.findViewById(R.id.editText1);
    edtTest.setText("Test text");
    final AlertDialog.Builder builder = new AlertDialog.Builder(context);

    builder.setTitle("Test");    
    btnTest.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String str=edtTest.getText().toString();
            builder.setMessage(str);
            builder.show();
        }
    });
  }

    return v;
}

希望它对你有用。

于 2013-10-11T12:11:51.660 回答