我的项目中定义了一个片段,还有一个带按钮的活动。我想要的是使用该按钮onclick listener启动片段,在片段的xml布局文件中定义的编辑文本中输入一个值,然后按片段中的另一个按钮2。现在我想要的是button2返回文本值并再次打开活动。然后我可以处理该值发生的变化。
package com.dreamgoogle.gihf;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class Kahinsebhii extends Fragment {
Button ok;
EditText number;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.kahin, container, false);
ok = (Button) view.findViewById(R.id.button1);
number = (EditText) view.findViewById(R.id.editText1);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
number.getText();
}
});
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/kahinbhiedit"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ok" />
</FrameLayout>
答案 0 :(得分:0)
听起来你需要的是Dialog
。看看开发网站:http://developer.android.com/guide/topics/ui/dialogs.html
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();