我的活动中有一个编辑文本,我想从片段中检查编辑文本是否为空。任何人都可以告诉我如何从android中的片段访问活动中存在的编辑文本视图组件。我这样做了但是没有用。 这是我的活动:
package com.developer.mainandroid;
import android.widget.EditText;
import com.milan.tabs.pagerslidingstrip.PagerSlidingTabStrip;
import com.milan.validation.lib.RegExpressionValueValidator;
import com.milan.validation.lib.ValidationManager;
import android.app.ActionBar;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.widget.SearchView;
import android.text.InputType;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.TextView;
public class Manual extends FragmentActivity {
private final Handler handler = new Handler();
private PagerSlidingTabStrip tabs_sliding_strip;
private ViewPager pager;
private Tests adapter_tests;
TextView welcome_headder;
String welcome_headder_string;
public EditText search_edit_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.manual);
ActionBar actionbar;
actionbar = getActionBar();
ColorDrawable color_action_bar = new ColorDrawable();
color_action_bar.setColor(Color.parseColor("#673AB7"));
actionbar.setBackgroundDrawable(color_action_bar);
search_edit_text = (EditText)findViewById(R.id.edt_number);
search_edit_text.setInputType(InputType.TYPE_CLASS_NUMBER);
search_edit_text.setGravity(Gravity.CENTER);
search_edit_text.setCursorVisible(false);
search_edit_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
search_edit_text.setCursorVisible(true);
}
});
tabs_sliding_strip = (PagerSlidingTabStrip)findViewById(R.id.pager_sliding_tab_strip);
pager = (ViewPager)findViewById(R.id.pager);
adapter_tests = new Tests(getSupportFragmentManager());
pager.setAdapter(adapter_tests);
final int page_margin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources().getDisplayMetrics());
pager.setPageMargin(page_margin);
tabs_sliding_strip.setTextColor(Color.parseColor("#FFFFFF"));
tabs_sliding_strip.setTabActiveTextColor(Color.parseColor("#673AB7"), Color.parseColor("#FFFFFF"));
tabs_sliding_strip.setViewPager(pager);
// tabs_sliding_strip.setTextColor(Color.parseColor("#FF00FF"));
}
public class Tests extends FragmentPagerAdapter{
private final String[] tests_types = {"BEFORE CONNECTING FUSE","AFTER CONNECTING FUSE","PISTON","FINAL AMMUNITATION"};
public Tests(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public CharSequence getPageTitle(int position) {
return tests_types[position];
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return tests_types.length;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ManualMode_BC_Fuse_ckt();
case 1:
return new Manual_AC_Fuse_ckt();
case 2:
return new piston();
case 3:
return new Manualmode_final();
}
return new ManualMode_BC_Fuse_ckt();
}
}
}
这是我的片段活动:
包com.developer.mainandroid;
import com.milan.material.widget.FloatingEditText;
import com.milan.validation.lib.RegExpressionValueValidator;
import com.milan.validation.lib.ValidationManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.graphics.Color;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
public class Manual_AC_Fuse_ckt extends Fragment {
Manual manual_child;
static FloatingEditText number_edit_text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Root_view = inflater.inflate(R.layout.manualmode_ac_fuse_ckt, null);
manual_child = new Manual();
number_edit_text = (FloatingEditText)getActivity().findViewById(R.id.edt_number);
registerForContextMenu(table_fuse_afer_cnc);
return Root_view;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("MANUAL MODE : AFTER FUSE CONNECTION");
menu.add(1, v.getId(), 0, "TEST ");
menu.add(1, v.getId(), 0, "SELECT ALL ");
menu.add(1, v.getId(), 0, "DESELECT ALL ");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="TEST ")
{
if(manual_child.search_edit_text.equals(""))
{
Toast.makeText(getActivity(), "Please enter number", Toast.LENGTH_LONG).show();
}
else{
//Do some operations
}
else if(item.getTitle()=="SELECT ALL ")
{
}
else if(item.getTitle()=="DESELECT ALL ")
{
}
return super.onContextItemSelected(item);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if(activity.getClass()==Manual.class){
manual_child = (Manual)activity;
}
}
}
这是我的Logcat:
01-02 03:07:43.170: E/AndroidRuntime(13424): FATAL EXCEPTION: main
01-02 03:07:43.170: E/AndroidRuntime(13424): java.lang.NullPointerException
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.app.Activity.findViewById(Activity.java:1853)
01-02 03:07:43.170: E/AndroidRuntime(13424): at com.developer.mainandroid.Manual_AC_Fuse_ckt.onCreateView(Manual_AC_Fuse_ckt.java:74)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.View.measure(View.java:15848)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.View.measure(View.java:15848)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.View.measure(View.java:15848)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
01-02 03:07:43.170: E/AndroidRuntime(13424): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.View.measure(View.java:15848)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-02 03:07:43.170: E/AndroidRuntime(13424): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.View.measure(View.java:15848)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.Choreographer.doFrame(Choreographer.java:532)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.os.Handler.handleCallback(Handler.java:730)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.os.Handler.dispatchMessage(Handler.java:92)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.os.Looper.loop(Looper.java:137)
01-02 03:07:43.170: E/AndroidRuntime(13424): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-02 03:07:43.170: E/AndroidRuntime(13424): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 03:07:43.170: E/AndroidRuntime(13424): at java.lang.reflect.Method.invoke(Method.java:525)
01-02 03:07:43.170: E/AndroidRuntime(13424): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-02 03:07:43.170: E/AndroidRuntime(13424): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 03:07:43.170: E/AndroidRuntime(13424): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
使用
EditText editText = getActivity().findViewById(R.id.EditText);
答案 1 :(得分:2)
经过与我的朋友在堆栈中的长时间讨论后,我找到了解决方案来访问Fragment中父活动内部存在的视图组件
在父活动中将视图组件设为 public static ,并确保相同包的片段活动
这是我的代码: 家长活动:
public class Sample extends FragmentActivity {
public static EditText meditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout....);
{
meditText = (EditText)findViewById(R.id.ed_1);
}
}
在片段活动中:
public class Example extends Fragment {
Sample sample_child;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Root_view = inflater.inflate(R.layout.....);
sample_child = new Sample();
if(sample_child.meditText.getText().toString().equals(""))
{
Toast.makeText(getActivity(), "Please Enter text or number", Toast.LENGTH_LONG).show();
}
return Root_view;
}
}
答案 2 :(得分:0)
您可以将edittext对象声明为静态,然后在片段中将其用作
Mainactivity.edittextobj.setText("Test");
答案 3 :(得分:0)
使用类似的验证: - 在主类: -
if (!(Validation.validateUsername(nameString))) {
editxt_name.setError(Html
.fromHtml("<font color='red'>Please enter username</font>"));
return;
在验证类中:
public static boolean validateUsername(String username) {
if (username.length() > 0) {
return true;
} else {
return false;
}
答案 4 :(得分:0)
简而言之,你可以做到这一点。
在您的framgment中,在onResume()中。呼叫
((YourActivityClass)getActivity()).edit_text
。
很长一段时间,您必须知道getActivity()
并不总是返回您的活动,有时它将返回null。所以你必须做两步。
public void onAttach (Activity activity) {
super.onAttach(activity);
if(activity.getClass()== YourActivity.class){
mActivity = (YourActivity) activity;
}
}
mActivity.edit_text
更新#1: 我将更改你片段的oncreateview
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Root_view = inflater.inflate(R.layout.manualmode_ac_fuse_ckt, null);
manual_child = new Manual();
number_edit_text = (FloatingEditText)getActivity().findViewById(R.id.edt_number);
registerForContextMenu(table_fuse_afer_cnc);
return Root_view;
}
进入这个
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Root_view = inflater.inflate(R.layout.manualmode_ac_fuse_ckt, null);
number_edit_text = (FloatingEditText)manual_child.findViewById(R.id.edt_number);
registerForContextMenu(table_fuse_afer_cnc);
return Root_view;
}
更新#2: 变化
if(manual_child.search_edit_text.equals(""))
{
Toast.makeText(getActivity(), "Please enter number", Toast.LENGTH_LONG).show();
}
进入这个
if(manual_child.search_edit_text.getText().toString().equals(""))
{
Toast.makeText(manual_child, "Please enter number", Toast.LENGTH_LONG).show();
}
答案 5 :(得分:0)
您可以覆盖手动课程中的方法:
@Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}
并调用其中的句子
更新---&GT;在Activity类手册中编写此方法:
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="TEST ")
{
if(manual_child.search_edit_text.equals(""))
{
Toast.makeText(getActivity(), "Please enter number", Toast.LENGTH_LONG).show();
}
else{
//Do some operations
}
else if(item.getTitle()=="SELECT ALL ")
{
}
else if(item.getTitle()=="DESELECT ALL ")
{
}
return super.onContextItemSelected(item);
}