我在点击登录(按钮)后尝试阅读w_number_edit_text
(文本框)的内容。但是在OnClick
方法上,我似乎无法访问w_number_edit_text
的内容。文本框和按钮都位于片段xml而不是main.xml
上。谁能告诉我如何解决这个问题。简而言之,如何在点击EditText
后访问Button
的内容?谢谢你
我的代码如下:
package com.example.medrec;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements OnClickListener{
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button login = (Button)rootView.findViewById(R.id.w_login);
login.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
//////////////////////////// Problem is here
EditText wNumbers =(EditText)findViewById(R.id.w_number_edit_text);
String wNumber = wNumbers.getText().toString();
Toast.makeText(getActivity(), wNumber, Toast.LENGTH_LONG).show();
}
}
}
我的activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.medrec.MainActivity"
tools:ignore="MergeRootFrame" />
我的fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.medrec.MainActivity$PlaceholderFragment" >
<EditText
android:id="@+id/w_number_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="118dp"
android:text="@string/w_number" />
<Button
android:id="@+id/w_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/w_number_edit_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="@string/w_login_text" />
</RelativeLayout>
答案 0 :(得分:2)
你应该改变这个
EditText wNumbers =(EditText)findViewById(R.id.w_number_edit_text);
使用
EditText wNumbers =(EditText)rootview.findViewById(R.id.w_number_edit_text);
和onCreateView(......)
如下所示:
EditText wNumbers
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button login = (Button)rootView.findViewById(R.id.w_login);
wNumbers =(EditText)rootView.findViewById(R.id.w_number_edit_text);
login.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String wNumber = wNumbers.getText().toString();
Toast.makeText(getActivity(), wNumber, Toast.LENGTH_LONG).show();
}
});
return rootView;
}
答案 1 :(得分:1)
在EditText
上初始化onCreateView
: -
public static class PlaceholderFragment extends Fragment implements OnClickListener{
public PlaceholderFragment()
{
}
EditText wNumbers
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
wNumbers =(EditText)rootView.findViewById(R.id.w_number_edit_text);
Button login = (Button)rootView.findViewById(R.id.w_login);
login.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
String wNumber = wNumbers.getText().toString();
Toast.makeText(getActivity(), wNumber, Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
答案 2 :(得分:1)
试试这个..
您无法在this
Fragment
内使用getActivity()
1}}
View rootView ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button login = (Button)rootView.findViewById(R.id.w_login);
login.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText wNumbers =(EditText)rootView.findViewById(R.id.w_number_edit_text);
String wNumber = wNumbers.getText().toString();
Toast.makeText(getActivity(), wNumber, Toast.LENGTH_LONG).show();
}
});
return rootView;
}
答案 3 :(得分:0)
在班级中定义rootview
。
并使用下面的EditText
检测您的rootView.findviewbyid
,
EditText wNumbers =(EditText)rootView.findViewById(R.id.w_number_edit_text);
onCreateView()
代码如下,
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button login = (Button)rootView.findViewById(R.id.w_login);
login.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
//////////////////////////// Problem is here
EditText wNumbers =(EditText)rootView.findViewById(R.id.w_number_edit_text);
String wNumber = wNumbers.getText().toString();
Toast.makeText(getActivity(), wNumber, Toast.LENGTH_LONG).show();
}