我按照这个教程对活动http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/如何使用与活动相同的片段?
创造一个良好的标签,但它使用的片段和片段没有不可能性使用任何正常的代码ES:
Toast.makeText(this, "Error...", Toast.LENGTH_LONG).show();
或一个简单的按钮点击
btnSum =(Button)findViewById(R.id.button4);
btnSum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAuthTask = new TaskAsincrono();
mAuthTask.execute((Void) null);
}
});
Fragment没有任何工作,它的可能性?
我需要的标签,因为活动有多重功能,结果是好的,但在级别的代码没有,1片段已登录与按钮,文本,但如果我叫按一下按钮不起作用例如:
Button btnSum =(Button)findViewById(R.id.button4);
btnSum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
片段TAB2:
<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:id="@+id/tab2"
tools:context="net.appsite.Tab2">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:layout_x="49dp"
android:layout_y="226dp"
android:id="@+id/editText8"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:layout_x="114dp"
android:layout_y="280dp"
android:elevation="0dp"
android:layout_marginTop="42dp"
android:layout_below="@+id/editText8"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Tab2.jar:
public class Tab2 extends Fragment{
public Tab2() {
// Required empty public constructor
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//ANY NORMAL CODE HERE NOT WORK, BUT IN NORMAL ACTIVITY WORK!
Toast.makeText(this, "Error...", Toast.LENGTH_LONG).show();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab2, container, false);
}
}
这是我与Android首次应用,普莱舍帮助我。
片段不延长'Context',因此你不能用'this'。如果您需要上下文,您可以使用'getContext()'或'getActivity()'。显示一个'吐司' –