我想将主活动中的整数数据发送到自定义对话框,此整数将帮助选择文本视图和按钮
此代码调用另一个活动中的自定义对话框
主要活动
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter=1;
CustomDialogClass cdd=new CustomDialogClass(MainActivity.this,getApplicationContext(),counter);
try{
Toast.makeText(MainActivity.this,"ExitCounter",Toast.LENGTH_LONG).show();
}catch (Exception e){
Toast.makeText(MainActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
}
//cdd.show();
}
});
hello = (Button) findViewById(R.id.test_hello);
hello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter=2;
CustomDialogClass cdd=new CustomDialogClass(MainActivity.this,getApplicationContext(),counter);
try{
Toast.makeText(MainActivity.this,"HelloCounter",Toast.LENGTH_LONG).show();
}catch (Exception e){
Toast.makeText(MainActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
}
// cdd.show();
}
});
这是自定义对话框的代码
package com.example.mhaad.test;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
class CustomDialogClass extends Dialog implements android.view.View.OnClickListener {
public Activity c;
public Button yes, no,ok;
public TextView hello,exit;
public int selection;
Context context;
public CustomDialogClass(Activity a, Context applicationContext,int con) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
this.context=applicationContext;
selection=con;
Toast.makeText(context,"Con",Toast.LENGTH_LONG).show();
show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Toast.makeText(context,"onCreate",Toast.LENGTH_LONG).show();
Toast.makeText(context,""+selection,Toast.LENGTH_LONG).show();
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_custom_dialog_box);
yes = (Button) findViewById(R.id.btn_yes);
no = (Button) findViewById(R.id.btn_no);
ok=(Button)findViewById(R.id.btn_ok);
exit = (TextView) findViewById(R.id.txt_dia);
hello = (TextView) findViewById(R.id.txt_dia1);
exit.setVisibility(View.INVISIBLE);
hello.setVisibility(View.INVISIBLE);
yes.setVisibility(View.INVISIBLE);
no.setVisibility(View.INVISIBLE);
ok.setVisibility(View.INVISIBLE);
yes.setOnClickListener(this);
no.setOnClickListener(this);
ok.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Toast.makeText(context,"Click",Toast.LENGTH_LONG).show();
switch(selection){
case 1:
Log.e("asd",String.valueOf(selection));
exit.setVisibility(View.VISIBLE);
hello.setVisibility(View.INVISIBLE);
yes.setVisibility(View.VISIBLE);
no.setVisibility(View.VISIBLE);
ok.setVisibility(View.INVISIBLE);
switch (v.getId()) {
case R.id.btn_yes:
c.finish();
break;
case R.id.btn_no:
dismiss();
break;
case R.id.btn_ok:
dismiss();
break;
default:
break;
}
break;
case 2:
Log.e("asd",String.valueOf(selection));
exit.setVisibility(View.INVISIBLE);
hello.setVisibility(View.VISIBLE);
yes.setVisibility(View.INVISIBLE);
no.setVisibility(View.INVISIBLE);
ok.setVisibility(View.VISIBLE);
switch (v.getId()) {
case R.id.btn_yes:
c.finish();
break;
case R.id.btn_no:
dismiss();
break;
case R.id.btn_ok:
dismiss();
break;
default:
break;
}
break;
default:
break;
}
dismiss();
}
}
这是清单
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
整数传递给自定义对话框但是在对话框结束后整数传递给onClick(),我已经尝试了所有东西,但onClick()函数不能访问int值。 需要帮助!!
答案 0 :(得分:0)
将整数传递给捆绑包。
//Create the bundle
Intent i=new intent(MainActivity.this,yourSecondActivity)
Bundle bundle = new Bundle();
//Add your integer
bundle.putInt("integer", your_integer);
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);
//在你的第二个活动中
Bundle bundle = getIntent().getExtras();
//Extract the data…
Integer int= bundle.getInt("integer",0);