在我的Android应用程序中,我正在为片段tabhost使用片段活动。在该片段活动中,我使用了一个edittext框,我无法编辑或在该edittext中输入文本。
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();
}
});
}
}
答案 0 :(得分: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;
}
希望它对你有用。