以下是我的活动。我点击两个不同的按钮通过Intent调用此活动。现在我想要的是当我打开下面的活动点击第一个按钮然后我想在edittext中显示提示" Po没有新的"当我点击第二个按钮打开下面的活动时,我想在edittext" Po No old"中显示提示。我怎么能解决这个问题?
Xml文件 -
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Po no."
android:textColor="@color/textColor"
android:textSize="13sp"
android:textStyle="bold" />
<EditText
android:id="@+id/txtPoNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="3"
android:imeOptions="actionDone"
android:inputType="number"
android:nextFocusDown="@+id/txtMeterSrMo"
android:singleLine="true"
android:textSize="14sp" />
</LinearLayout>
</ScrollLayout>
活动文件 -
public class InstallationDashBoardActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_activity);
txtPoNo = (EditText) findViewById(R.id.txtPoNo);
}
}
按钮单击 -
installation.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ServiceCategories.this,InstallationDashBoardActivity.class);
intent.putExtra("isNew", true);
startActivity(intent);
}
});
replacement.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ServiceCategories.this,InstallationDashBoardActivity.class);
intent.putExtra("isNew", false);
startActivity(intent);
}
});
我将代码放在我的片段onCreateView -
中//Getting Extras
if(getActivity().getIntent().getExtras()!=null){
isNew = getActivity().getIntent().getExtras().getBoolean("isNew");
}
// Added condition here
if(isNew) {
txtPoNo.setHint("Po No New");
}
else {
txtPoNo.setHint("Po No Old");
}
答案 0 :(得分:0)
致电
txtPoNo.setHint("Po No Old");
<强>更新强>
你的意思是你想要检测点击了哪个按钮,并根据你要显示正确的提示?对于那个非常简单的传递一个带有intent的布尔标志,并尝试使用该布尔值
进行识别installation.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ServiceCategories.this,InstallationDashBoardActivity.class);
//Added flag here
intent.putExtras("isNew", true);
startActivity(intent);
}
});
replacement.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ServiceCategories.this,InstallationDashBoardActivity.class);
//Added flag here
intent.putExtras("isNew", false);
startActivity(intent);
}
});
在您的活动中获取布尔值
public class InstallationDashBoardActivity extends AppCompatActivity {
//boolean added
public boolean isNew;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_activity);
//Getting Extras
if(getIntent().getExtras()!=null){
isNew = getIntent().getExtras().getBoolean("isNew");
}
txtPoNo = (EditText) findViewById(R.id.txtPoNo);
// Added condition here
if(isNew)
txtPoNo.setHint("Po No New");
else
txtPoNo.setHint("Po No Old");
}
}
答案 1 :(得分:0)
installation.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ServiceCategories.this, InstallationDashBoardActivity.class);
intent.putExtra("key", "value");
startActivity(intent);
}
});
replacement.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ServiceCategories.this, InstallationDashBoardActivity.class);
startActivity(intent);
}
});
和第二项活动
Intent intent2 = getIntent();
String strKey = intent2.getStringExtra("key");
if (strKey.equalsIgnoreCase("value")) {
txtPoNo.setHint("Po No New");
} else {
txtPoNo.setHint("Po No Old");
}
答案 2 :(得分:0)
editText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
editText.setHint("");
return false;
}
});
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
editText.setHint("Hint");
}
}
});