gpt4 book ai didi

android - 访问 EditText Android

转载 作者:行者123 更新时间:2023-11-30 02:55:41 25 4
gpt4 key购买 nike

我有一个带有 activity_main 布局的 MainActivity。在 activity_main 布局中我有 4 个 fragment 。在其中一个 fragment 中,我有 2 个文本框和 1 个按钮。它是一个登录表单。当我单击此按钮时,我想知道文本框中的文本是什么。在 MainActivity 中,我用 findviewbyid 定义了文本框。但是当我点击按钮时,我无法在 TextView 中获取文本。

我该怎么办?

主要 Activity :

<TableLayout 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.decoration.MainActivity"
tools:ignore="MergeRootFrame"
android:background="#FFFFFF"
android:orientation="horizontal"
android:focusableInTouchMode="true">

<TableRow android:layout_weight="1"
android:gravity="top">


<fragment
android:id="@+id/UpLeft"
android:name="com.example.decoration.UpLeftFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left" />




<fragment
android:id="@+id/UpRight"
android:name="com.example.decoration.UpRightFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right" />


</TableRow>


<TableRow android:layout_weight="1"
android:gravity="bottom" >



<fragment
android:id="@+id/DownLeft"
android:name="com.example.decoration.DownLeftFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left" />



<fragment
android:id="@+id/DownRight"
android:name="com.example.decoration.DownRightFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right" />

</TableRow>

down_left_fragment :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="#761F01"
android:orientation="vertical" >

<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:scaleType="fitXY"
android:src="@drawable/bottomleft" />

<EditText
android:id="@+id/Usernme"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@drawable/txtedges"
android:gravity="center"
android:hint="Username"
android:inputType="text"
android:singleLine="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="3dp"/>

<EditText
android:id="@+id/Password"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@drawable/txtedges"
android:gravity="center"
android:hint="Password"
android:inputType="textPassword"
android:singleLine="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="3dp"/>

<Button
android:id="@+id/SignInBtn"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@drawable/btnedges"
android:text="SignIn"
android:textColor="#FFFFFF" />

<CheckBox
android:id="@+id/Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:checked="true"
android:gravity="right|end"
android:text="Save"
android:textColor="#F7BA00" />

DownLeftFragment.java :

public class DownLeftFragment extends Fragment{

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.down_left_fragment, container, false);
}
}

主要 Activity :

public class MainActivity extends Activity {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText a = (EditText) findViewById (R.id.Username);
Button Login = (Button) findViewById(R.id.SignInBtn);
Login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
StrictMode.setThreadPolicy(policy);
String n = a.getText().toString();
login(n);
}
});


public void login(String a)
{...
}

最佳答案

不要在 onClick() 中获取对 EditText 的新引用,因为它将与用户完成输入的对象不同.

此外,正如 ElDuderino 所建议的,如果您有 Fragment 的布局,请在您的 Fragment 中获取带有 findViewById(..) 的 View 实例。这样,如果不再需要 View,它可以与 Fragment 一起被收集起来。否则只要 Activity 存在,它就会一直存在。

编辑:

在你的 fragment 中试试这个:

public class DownLeftFragment extends Fragment {

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View root = inflater.inflate(R.layout.down_left_fragment, container, false);
EditText edit = (EditText) findViewById (R.id.Username);
Button login = (Button) findViewById(R.id.SignInBtn);
login.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
StrictMode.setThreadPolicy(policy);
String n = edit.getText().toString();
login(n);
}
});

return root;
}
public void login(String a) {
...
}
}

您的 MainActivity 现在看起来像这样:

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
}

这样所有的 View 都属于扩展特定布局的 fragment 。

关于android - 访问 EditText Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23309792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com